Same Code, Two Systems, Different Result (C++, strtol, and sprintf)

Status
Not open for further replies.
Hi, is there anything in this function that would cause it to return {1,2,3,4} on one system and {11,12,13,14} on another? Both have the same environment variable of TEST=0xb. When I run it on my computer it gives the correct output: 0xb is translated to 11, found to be odd, and the string {11,12,13,14} is computed.

But when I run it on another computer, somehow I wind up with {1,2,3,4}

I want to make sure I'm not missing something obvious here.




void example()
{

string str;
char temp[11];
int number = 0;

const char *penv = getenv( "TEST" );

number = strtol( penv, NULL, 16 );

if((number % 2) == 1) //Odd #
sprintf( temp, "%d,%d,%d,%d", number, number+1, number+2, number+3);
else
sprintf( temp, "%d,%d,%d,%d", number-1, number, number+1, number+2);

str = temp;


cout << str << "\n";

}
 

Jonathon

Ars Legatus Legionis
17,646
Subscriptor
'int' and 'long int' aren't guaranteed to be the same (for example, on 64-bit Linux, long is 64 bits, while int is 32 bits). Your code won't compile on a system where they're different, so you should change 'number' to be a long instead of an int or use atoi instead (or explicitly cast it from long to int when you assign it, but that will mask possible issues if your number goes out of range).

And, yes, your test buffer is one byte too small for the sample output you've given. Remember that you have to allocate memory for the null terminator at the end of the string (so, you need to allocate 1 + the number of possible characters you could have in the string).

Neither of those is likely to be the root cause of the issue you're seeing, though. Off the top of my head, nothing stands out as wrong about that program, aside from the type mismatch and the buffer overflow. Are you sure you're setting the environment variable correctly (and exporting it, if on *nix)?

Going off on a tangent: if you're coding C++, you really shouldn't be using C strings and sprintf (or that whole family of functions) for anything except where absolutely required to interoperate with pure C (which is actually pretty rare). The C++ string class and ostringstream are your friends (and you can even swap strtol with istringstream if you want); it's considerably easier to write stable and secure code in the pure C++ world than it is in the pure C world.
 
Even on systems where int and long are the same width they are still distinct types, so if long couldn't be implicitly converted to an int it would always be an error, even on systems where they're both the same width. For example, int i = 1l << 40; is legal on systems where long is 64bits and int is 32 bits. Specifically, the resulting value is implementation defined [C++11 § 4.7 p3]. This code results in a warning when using -Wconstant-conversion on clang (and probably gcc), and since we all know everyone uses -Werror, that warning becomes an error. However this is only because the value is available at compile time. There may be some other warning that will flag all of these narrowing conversions even for non-constant variables.
 
Status
Not open for further replies.