[libc-commits] [PATCH] D112176: [libc] fix strtol returning the wrong length

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Oct 20 23:13:43 PDT 2021


sivachandra added inline comments.


================
Comment at: libc/src/__support/str_conv_utils.h:83
+    seen_digit = true;
+  }
+
----------------
michaelrj wrote:
> sivachandra wrote:
> > Do we need this `if` block at all?
> it's to handle the case where the number is just "0" and the base is set to automatic. Without this, then that would be parsed as an octal number with no digits, as opposed to a decimal number with one digit. I've added a test to check this.
> The other part of the condition was unnecessary though.
May be `infer_base` is incorrect then? Consider this:

```
const char *n = "08";
char *next;
long i = ::strtol(n, &next, 0);
```

Since "08" is an invalid number, should we want the above `strtol`call  to succeed or a fail? If it is a failure, should `next` point to 8 or 0? Should it be the same if the base was explicitly specified to be 8?

I also think we can ask the same questions when base is 16.

My reading of the standard says that "08" is an invalid number so no conversion should be performed. Which means, `str_end` should point to `original_src`. Likewise, "0xZ" with base 16 is an invalid number so no conversion should be performed and `str_end` should point to `original_src`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112176/new/

https://reviews.llvm.org/D112176



More information about the libc-commits mailing list