Opened 6 years ago

Closed 6 years ago

#13949 closed bug (fixed)

strtod() should only advance *endptr one byte given "0x"

Reported by: i80and Owned by: nobody
Priority: low Milestone: Unscheduled
Component: System Version: R1/Development
Keywords: Cc:
Blocked By: Blocking:
Platform: All

Description

#include <stdio.h>
#include <stdlib.h>
int main(void) {
  const char* str = "0x";
  char* endp;
  strtod(str, &endp);
  printf("%ld\n", endp - str);
}

On other platforms, this will print 1. On Haiku, it prints 2.

From http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtod.html:

The expected form of the subject sequence is an optional '+' or '-' sign, then one of the following: ...

  • A 0x or 0X, then a non-empty sequence of hexadecimal digits...

Other platforms do not advance *endptr to indicate that the 'x' was consumed, which seems like preferable behavior because it lets the caller know that the input string was odd.

Attachments (1)

0001-strtod-Do-not-consume-x-in-incomplete-hex-input.patch (2.3 KB ) - added by i80and 6 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 by waddlesplash, 6 years ago

We haven't updated the Contributing guide just yet, but we have a Gerrit instance live at https://review.haiku-os.org -- so if you could submit your patch there, that would be best.

comment:2 by waddlesplash, 6 years ago

Also, is this how upstream glibc solved the problem? If so, could you link to their fix commit?

comment:3 by i80and, 6 years ago

This is not even close to how upstream glibc works. Unfortunately, they've diverged significantly from what exists in the haiku tree.

Would it be better to try and sync the implementation with upstream?

comment:4 by i80and, 6 years ago

Made a new patch, using the following two diffs from upstream glibc:

I'll try to figure out Gerrit

comment:5 by waddlesplash, 6 years ago

Resolution: fixed
Status: newclosed

Merged patch from Gerrit. Thanks!

Note: See TracTickets for help on using tickets.