• 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

Quote from: Skywing on December 01, 2003, 06:05 PM
Quote from: iago on December 01, 2003, 05:39 PM
Then the code works fine.  Skywing is crazy.  
Simply because it appears to work fine doesn't mean that nothing is wrong.

It does, I'm afraid.  So you lose.  :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Grok

Skywing is correct.  You're not thinking.

DarkMinion

Maybe because you're outputting it as unsigned when it returns a signed integer?

DarkMinion

Also, getc() returns a signed integer as well...could lead to certain problems

Skywing

Quote from: DarkMinion on December 01, 2003, 09:09 PM
Maybe because you're outputting it as unsigned when it returns a signed integer?
That's not the problem in question.  Outputting an int as unsigned isn't the cause of any breakage here.

DarkMinion

Maybe it would help if you said what problem you were experiencing?  Ballpark?

iago

To quote the man page:
QuoteGETS(3)             Linux Programmer's Manual             GETS(3)

NAME
      fgetc, fgets, getc, getchar, gets, ungetc - input of char-
      acters and strings

SYNOPSIS
      #include <stdio.h>
[...]
      int getc(FILE *stream);

getc returns and int and your storing it in a char.  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*


Skywing

Quote from: DarkMinion on December 01, 2003, 09:13 PM
Maybe it would help if you said what problem you were experiencing?  Ballpark?
The problem isn't related to the printf call failing or outputting the return value of isspace incorrectly because I used %u.  For instance, the same problem would occur if you were using C++ and did std::cout << isspace(ch) << endl;.  I'm not sure what else I can say without giving it away.

DarkMinion

Bad iago copying what I just said.

iago

Quote from: DarkMinion on December 01, 2003, 09:18 PM
Bad iago copying what I just said.

Not really.. I actually said what the problem IS.  :P
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

Note that the topic is What can go wrong with this code? and not What does this code do wrong?.  If you see something that you think is wrong, you should be able to tell about the consequences of that and soforth...

iago

Well, if getc returns something greater than 255 (I don't know how this would happen, but it CAN since it's an <int>), it will overflow the char.  I don't think anything with break, besides getting an invalid value in ch.
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

#27
Quote from: iago on December 01, 2003, 09:22 PM
Well, if getc returns something greater than 255 (I don't know how this would happen, but it CAN since it's an <int>), it will overflow the char.  I don't think anything with break, besides getting an invalid value in ch.
I think that in the case of an overflow, ch will get the return value of getc modulo max_value_of_char, possibly adjusting for signedness.  In any case, I don't think it'll have an "invalid" value (assume all values of a char are "valid" characters).

DarkMinion

Actually iago, you really did just say what I just said :P

But according to Sky, it's wrong

iago

#29
True, but if you press character 0x120 (look carefully, it's in the corner), and it modulos it by sizeof(char), it will incorrectly identify it as a space.

Another thing I was thinking, \n is made up of 2 characters on some computers; will that affect the output, or is 0x0d 0x0a read in as a single character?

[edit] fixed \n hex.. oops :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


|