This is an archive of the discontinued LLVM Phabricator instance.

[libc][NFC] make atoi undefined cases match std
ClosedPublic

Authored by michaelrj on Dec 19 2022, 1:39 PM.

Details

Summary

The standard describes atoi as:

"equivalent to atoi: (int)strtol(nptr, (char **)NULL, 10)"

Previously, our behavior was slightly different on numbers larger than
INT_MAX, but this patch changes it to just do the cast instead. Both of
these are valid since the standard says

"If the value of the result cannot be represented, the
behavior is undefined."

But matching existing behavior makes differential fuzzing easier.

Diff Detail

Event Timeline

michaelrj created this revision.Dec 19 2022, 1:39 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptDec 19 2022, 1:39 PM
michaelrj requested review of this revision.Dec 19 2022, 1:39 PM
sivachandra accepted this revision.Dec 19 2022, 3:56 PM
sivachandra added inline comments.
libc/test/src/stdlib/AtoiTest.h
68

Nit: Make this conditional more explicit:

cpp::is_same_v<ReturnT, int> && sizeof(ReturnT) < sizeof(long)
69

Nit: I think this assume int to be 4 bytes wide so add: static_assert(sizeof(int) == 4).

This revision is now accepted and ready to land.Dec 19 2022, 3:56 PM
michaelrj marked 2 inline comments as done.

address comments and rebase before landing

This revision was landed with ongoing or failed builds.Dec 20 2022, 10:21 AM
This revision was automatically updated to reflect the committed changes.