This is an archive of the discontinued LLVM Phabricator instance.

[libc] fix strtointeger hex prefix parsing
ClosedPublic

Authored by michaelrj on Sep 1 2021, 1:12 PM.

Details

Summary

Fix edge case where "0x" would be considered a complete hexadecimal
number for purposes of str_end. Now the hexadecimal prefix needs a valid
digit after it, else just the 0 will be counted as the number.

Diff Detail

Event Timeline

michaelrj created this revision.Sep 1 2021, 1:12 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 1 2021, 1:12 PM
michaelrj requested review of this revision.Sep 1 2021, 1:12 PM
sivachandra accepted this revision.Sep 2 2021, 6:13 PM

While it is OK for this patch, I would suggest that the tests be templatized.

libc/src/__support/str_conv_utils.h
82

I think the logic here and on line 41/42 can be moved to a single function say, is_hex_start(...). Then, we can use it like this:

else if (base == 16 && is_hex_start(src)) {
  ...
}

infer_base will have to be modified to something like this:

if (is_hex_start(*src)) {
  (*src) += 2;
  return 16;
} else if (**src == '0') {
  ++(*src);
  return 8;
} else {
  return 10;
}
This revision is now accepted and ready to land.Sep 2 2021, 6:13 PM
michaelrj updated this revision to Diff 370626.Sep 3 2021, 10:40 AM
michaelrj marked an inline comment as done.

move the hex number identification to a function

This revision was automatically updated to reflect the committed changes.