This is an archive of the discontinued LLVM Phabricator instance.

[obj2yaml] - Stop triggering UB when dumping corrupted strings.
ClosedPublic

Authored by grimar on Oct 18 2019, 1:57 AM.

Details

Summary

We have a following code to find quote type:

if (isspace(S.front()) || isspace(S.back()))

Problem is that:

"int isspace( int ch ): The behavior is undefined if the value of
ch is not representable as unsigned char and is not equal to EOF."
(https://en.cppreference.com/w/cpp/string/byte/isspace)

This patch shows how this UB can be triggered and fixes an issue.

Diff Detail

Event Timeline

MaskRay accepted this revision.Oct 18 2019, 8:08 AM

Thanks!

test/tools/obj2yaml/invalid-section-name.yaml
31 ↗(On Diff #225573)

Does a shorter sequence work?

This revision is now accepted and ready to land.Oct 18 2019, 8:08 AM
rupprecht accepted this revision.Oct 18 2019, 2:46 PM
rupprecht added inline comments.
test/tools/obj2yaml/invalid-section-name.yaml
3 ↗(On Diff #225573)

nit: remove "do"

grimar marked an inline comment as done.Oct 20 2019, 8:50 AM
grimar added inline comments.
test/tools/obj2yaml/invalid-section-name.yaml
31 ↗(On Diff #225573)

For this particular test "00FE00" works fine.

But in this case another sections produced (.strtab, .shstrtab) get a sh_name larger than the section name string table size though.
e.g. sh_name=5.

Then if we use "llvm-readobj -a" for the object produced, we'll have an error like
"error: '1.o': a section [index 3] has an invalid sh_name (0xf) offset which goes past the end of the section name string table"

A minimal sequence that allowes llvm-readobj -a to pass is

Content: "00FEFEFEFEFEFEFEFEFEFEFEFEFEFE00"

I wasn't sure what to use here. I had a feeling that it is a bit tricky place and is not the best candidate for optimization,
so even added s few more "FE" just in case.

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptOct 21 2019, 3:40 AM
jhenderson added inline comments.Oct 28 2019, 9:57 AM
llvm/test/tools/obj2yaml/invalid-section-name.yaml
2–4

It looks like @rupprecht's comment (remove "do") here wasn't addressed prior to commit. I also want to suggest a couple of other minor improvements to the comment:

"Here we replace the section name..."
"We used to assert for..."

grimar marked an inline comment as done.Oct 28 2019, 10:29 AM
grimar added inline comments.
llvm/test/tools/obj2yaml/invalid-section-name.yaml
2–4

It looks like @rupprecht's comment (remove "do") here wasn't addressed prior to commit.

Yes, it was forgotten for a short time, but I addressed it in the follow-up: rL375405.

I'll apply your suggestions tomorrow.