Friday, April 17, 2009

_wtoi64 is broken for large values

I recently found a bug in _wtoi64 on Dev Studio 2005. When passed some values larger than 9,223,372,036,854,775,807 (i.e. the high bit of the 64-bit word is set), the returned value will have some bits flipped. This manifested itself in one of our internal tools.

Workaround? Use sscanf (the wide-char version, however you spell it) with %I64d or %I64u. For some strange reason, sscanf and _wtoi64 don't share any logic for translating strings to values. Go figure.

Good job, Microsoft. *golf clap*

1 comment:

  1. Did some more research here. Looks like when values exceed I64_MAX (the signed maximum), it returns I64_MAX and sets errno to ERANGE. That's what happened in this case.

    So, not as bad as it first appears, but I like the behavior of sscanf better. And who checks errno after calling atoi64?

    ReplyDelete