March 22, 2009

Sundays, Numeric Literals

Programming languages generally allow you to deal with numeric values using a literal syntax. In Ruby (irb), for example:

» 5*5
=> 25

This is a literal expression of multiplying 5 by 5, resulting in 25. Often, languages also provide a literal syntax for expressing numbers of a different number system. Consider this snippet of Java code that creates three integer primitives, each with a decimal value of 15:

int iDec = 15;    // decimal
int iOct = 017;  // octal (base 8)
int iHex = 0xF;  // hexadecimal (base 16)

I was poking around some legacy PHP that has been lurking around my company for some time. I was doing this because we’re seriously considering an exciting new direction that would include deploying legacy PHP in a Java servlet container, via Quercus. In one application, the Quercus interpreter fell over with a deceptive error message when it encountered a specific numeric literal: 078051120. In the end I see this as a good thing, because it alerted me of a bug in our software. This literal was part of an array declared to contain a list of known bogus social security numbers. The programmer had intended for this value to represent the would-be SSN of ‘078051120.’ However, that is wrong and I’m glad Quercus broke (where the PHP C implementation happily moves along).

PHP, like Java, supports literal numeric values in different number systems. Therefore, any literal starting with the digit ‘0’ is interpreted as an octal value. The stupid thing about how PHP handles a literal like ‘078051120’ is that it doesn’t complain at all about the number ‘8.’ The octal system is base 8, and so the only valid digits are 0, 1, 2, 3, 4, 5, 6, and 7.

The other frustrating part is the mistake. Even if the example were a valid octal literal (like 077051120), the code would still be horribly broken. This is because the programmer expected the value to represent the SSN of 077051120, when in reality it represented the decimal value of 16536144.

Be aware of how your chosen languages handle numeric literals!

4:34pm  |   permalink
FILED UNDER: php programming mistakes