LWN.net Logo

A turning point for GNU libc

A turning point for GNU libc

Posted Mar 29, 2012 0:02 UTC (Thu) by Richard_J_Neill (subscriber, #23093)
In reply to: A turning point for GNU libc by cmorgan
Parent article: A turning point for GNU libc

While we're at it, could we please get rid of the broken behaviour that leading zeros make strings into Octal? This behavior is implemented so fundamentally in eg atoi() that many other programming languages, even those that do dynamic typing, inherit the bug. If I write "064", I generally don't mean "52"!

A leading zero practically NEVER means the user intentionally wants to work in base 8, it just means they did something naive with string-splitting or data-entry. Perhaps the user entered the reading from a digital-scale, complete with leading zero. Or they took a string such as $7.09 and parsed it as "trim off the leading '$', split at the decimal point, multiply the first number by 100, and add the two together, to get a result in cents".

I'd consider that leading-zero-means-octal is a nasty case of technical debt that we still be causing bugs in 100 years if we don't fix it.

One way would be to support a new notation, similar to the "0x" notation. Numbers beginning "0o" (that's a letter 'o') would be recognised as octal. Then we spend 5 years transitioning the existing legitimate instances of octal numbers (file permissions) to the new syntax, then for 5 years, make the old format illegal, then in 10 years time, we can start treating leading zeros as they were meant to be treated.

[At the same time, can I plead for a "streq()" function, being defined as "!strcmp()" ]


(Log in to post comments)

A turning point for GNU libc

Posted Mar 29, 2012 4:52 UTC (Thu) by slashdot (guest, #22014) [Link]

Are you sure about atoi supporting octal?

AFAICT POSIX 2008 forbids that, and requires atoi to support only decimal numbers.

At any rate, changing that is probably highly unwise, as it might break stuff.

A turning point for GNU libc

Posted Mar 29, 2012 10:37 UTC (Thu) by Richard_J_Neill (subscriber, #23093) [Link]

D'oh! You're quite right about atoi(). I meant strtol(), but wasn't thinking straight. strtol() allows you to optionally specify the base=10, but then won't permit 0x... for hexadecimal.

I agree that we can't change it right away. That's why I suggest adding a new prefix, "0o" to be used in the very rare case where the programmer deliberately intends to use octal. That wouldn't break anything, and over perhaps 5 years, people could migrate.

A turning point for GNU libc

Posted Mar 29, 2012 20:53 UTC (Thu) by cmccabe (guest, #60281) [Link]

> While we're at it, could we please get rid of the broken behaviour
> that leading zeros make strings into Octal? This behavior is
> implemented so fundamentally in eg atoi() that many other programming
> languages, even those that do dynamic typing, inherit the bug. If I
> write "064", I generally don't mean "52"!

It's a standard, like the QWERTY keyboard and the English language. Sorry, it's not going anywhere.

> At the same time, can I plead for a "streq()" function, being defined
> as "!strcmp()"

Why don't you define it yourself? It's a one-line function.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds