• Welcome to Valhalla Legends Archive.
 

What can go wrong with this code?

Started by Skywing, November 30, 2003, 11:33 PM

Previous topic - Next topic

iago

I dont' see how that would create a problem, though, since it still correctly identifies everything that the documentation says it will...
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

Quote from: iago on December 03, 2003, 01:42 AM
I dont' see how that would create a problem, though, since it still correctly identifies everything that the documentation says it will...
The operation of the function is undefined when given a negative integer that is not EOF.  This means that it could possibly identify your input as whitespace even if it is not whitespace, or even crash.

Consider a simple implementation somewhat like this...:


int isspace(int ch)
{
char table[255] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ...};
if(ch == EOF) ...
else return table[ch];
}


If you pass a negative value that isn't EOF, it will be reading from memory that isn't in the table.

Of course, exactly what will happen depends on how your particular isspace implementation works, but it probably won't be what you want.

iago

Wouldn't it correctly idenfify the -1 as EOF?  Assuming you're using that as a terminator..
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

Quote from: iago on December 03, 2003, 07:19 AM
Wouldn't it correctly idenfify the -1 as EOF?  Assuming you're using that as a terminator..
"If you pass a negative value that isn't EOF, it will be reading from memory that isn't in the table."

iago

hm, I didnt' read your post right.  I see what you mean now!

I didn't notice you declared table[255].  You're right, then, that if you try to reference a character >127, it'll reference a negative value.

That was my bad :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

The definition for the isspace I checked didn't mention not supporting negative numbers. It just said it will return true for those particular numbers and false for all others.

iago

I agree with Adron here.  If you had given your more recent code instead of using isspace, it would have been more clear.  As it was, everything acted completely predictably in the manner that the functions involved say they will.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


|