This is an archive of the discontinued LLVM Phabricator instance.

Fix llvm-strings crash for negative char values
ClosedPublic

Authored by jhenderson on Oct 22 2018, 9:33 AM.

Details

Summary

On Windows at least, llvm-strings was crashing if it encountered bytes that mapped to negative chars, as it was passing these into std::isgraph and std::isblank functions, resulting in undefined behaviour. On debug builds using MSVC, these functions verfiy that the value passed in is representable as an unsigned char. Since the char is promoted to an int, a value greater than 127 would turn into a negative integer value, and fail the check. Using the llvm::isPrint function is sufficient to solve the issue.

Diff Detail

Repository
rL LLVM

Event Timeline

jhenderson created this revision.Oct 22 2018, 9:33 AM
ruiu added inline comments.Oct 22 2018, 9:39 AM
include/llvm/ADT/StringRef.h
93–97 ↗(On Diff #170443)

Not sure if this is a good thing to do. If you do, you probably should do the same for StringRef::StringRef(const char *Str) and other functions? But isn't that a bit too much?

dexonsmith added inline comments.Oct 22 2018, 11:08 AM
include/llvm/ADT/StringRef.h
93–97 ↗(On Diff #170443)

If you do, you probably should do the same for StringRef::StringRef(const char *Str) and other functions?

IMO StringRef::StringRef(const char *Str) should only be used for string literals. Same with StringRef::withNullAsEmpty().

efriedma added inline comments.
tools/llvm-strings/llvm-strings.cpp
84 ↗(On Diff #170443)

Could we just use something like if (isPrint(*P) || *P == '\t') to avoid the issue? std::isgraph has other issues (see r338034).

jhenderson added inline comments.Oct 23 2018, 1:58 AM
tools/llvm-strings/llvm-strings.cpp
84 ↗(On Diff #170443)

Yes, I think we can, and that then solves the need for the new StringRef constructor. I'll update.

jhenderson retitled this revision from Add unsgined char StringRef constructor/Fix llvm-strings crash to Fix llvm-strings crash for negative char values.
jhenderson edited the summary of this revision. (Show Details)
jhenderson added subscribers: andrewng, edd, bd1976llvm and 2 others.

Remove unsigned char StringRef constructor, use llvm::isPrint, and improve test to show that the negative character is not printed.

jhenderson marked an inline comment as done.Oct 23 2018, 2:52 AM

LGTM for what that's worth, but I'll leave it for those who commented earlier to do the final approval.

ruiu accepted this revision.Oct 23 2018, 5:32 AM

LGTM

This revision is now accepted and ready to land.Oct 23 2018, 5:32 AM
jhenderson updated this revision to Diff 170620.EditedOct 23 2018, 6:17 AM

Add quotes to echo string in test. This fixes the test on Linux.

This revision was automatically updated to reflect the committed changes.