Re: Won't work without useless declarations



"John Bode" <john_bode@xxxxxxxxxxx> a écrit dans le message de news:
1190037154.676654.7560@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Sep 15, 10:18 pm, Lars Eighner <use...@xxxxxxxxxxxxxxx> wrote:
In the code below, the line "unsigned int ktop,kbot;"
seems to be useless as ktop and kbot are never used. Yet,
this work with it and breaks without it.

Any idea why?

<snip>

int getkey(void)
{
struct termios unwhacked, whacked;
unsigned int ktop,kbot;
char *k;

[snip]

read (0,&k,5);

This is your problem right here. At this point, k hasn't been
initialized to point anywhere meaningful; you've invoked undefined
behavior by writing to a random memory address, which in this case
appears to be the memory immediately preceding k itself. Bad juju.

close, but no cigar. OP passes the address of the uninitialized pointer k
and a length of 5. read will attempt to read 5 bytes into the pointer
itself and whatever follows. If pointers are 32 bits, some other variable
will be clobbered (in this case the unused ktop or kbot) or even worse, the
return address or the saved frame pointer will be changed causing undefined
behaviour (crash is likely). This line is definitely what is causing the
problem and this is the explaination for the curious side effect of removing
the unused variables.

You would be better off making k a static array instead of a pointer:

char k[5]; // or however big k needs to be
...
read(0, k, sizeof k);

Note that you don't need to use the address-of (&) operator on k, as
an array identifier in this context is converted to a pointer type.

Yes that is a good fix. Except for the vocabulary: char k[5] is by no means
a 'static array', it is an array with automatic storage, as such it is
uninitialized.
read's return value should be checked to verify how many bytes have actually
been read.

Oh, and the line

fflush(0);

is a problem; fflush() is not defined for input streams (the operation
is meaningless for input). Lose it.

Again, good point, but irrelevant.
fflush(stdin) is useless, or at best implementation defined.
fflush(0) or equivalently fflush(NULL) flushes all FILEs currently open for
output.

--
Chqrlie.


.



Relevant Pages

  • Re: Wont work without useless declarations
    ... Christopher Benson-Manica wrote: ... seems to be useless as ktop and kbot are never used. ...
    (comp.lang.c)
  • Re: Wont work without useless declarations
    ... seems to be useless as ktop and kbot are never used. ... It's still illegal standard C coding. ...
    (comp.lang.c)
  • Re: Wont work without useless declarations
    ... seems to be useless as ktop and kbot are never used. ... char kstr; ...
    (comp.lang.c)
  • Re: WM_COPYDATA ... whats inside?
    ... pointer to an internal copy of the data to the destination and returns ... AFTER the message was processed (whitch would render it pretty useless ... If you want, your receiver can copy the data, post a message to itself, and return immediately. ... The actual work can be done in the PostMessagehandler. ...
    (microsoft.public.vc.mfc)
  • WM_COPYDATA ... whats inside?
    ... copies the data in an internal buffer ... pointer to an internal copy of the data to the destination and returns ... AFTER the message was processed (whitch would render it pretty useless ...
    (microsoft.public.vc.mfc)