Re: low-level question



>I couldn't get this on a midterm. Darn!
>
>Anyone want to help?
>
>1. Why does the following program output a 0?

Who says it does? The standard certainly doesn't require it.

>2. Explain in detail.

There are no guarantees that if you overflow an array, it will overflow
into the variable declared immediately after it. (On some linkers,
it will overflow into the variable *alphabetically* after it, given
that they're both auto variables in the same function.) There are also
no guarantees that this code is running on an endian machine.

>3. How could you prevent this outcome without changing the code?

Don't run it? Run on a non-endian machine? Kill the author
of the code?

>
>
>#include <stdio.h>
>#include <string.h>
>
>int
>main( int argc, char *argv[] )
>{
> int ii = 1;
> char buf[ 4 ];
>
> strcpy( buf, "AAAA" );
>
> printf( "%d\n", ii );
>
> return 0;
>}
>


.



Relevant Pages

  • Re: size_t overflow
    ... than overflow. ... question is about size_t arithmetics. ... means all computations involving unsigned char promoted to int, ... After promotion to int, ...
    (comp.std.c)
  • Re: dynamic vs. static: the age-old debate
    ... int addx+y; ... orthogonal to static/dynamic/soft typing. ... $ dynamic-compiler program.source -o program ... will overflow, leaving the "overflow behavior is ...
    (comp.lang.misc)
  • Re: size_t overflow
    ... than overflow. ... question is about size_t arithmetics. ... ..."A computation involving unsigned operands can never overflow, ... means all computations involving unsigned char promoted to int, ...
    (comp.std.c)
  • ishopcart cgi 0day and multiple vulnerabilities
    ... While spending a night auditing I have found 2 buffer overflows and 1 ... there is an overflow in the vGetPostfunction, it does not do any size checking on the inputed data but instead ... void changeport(char *code, int port, int offset); ...
    (Bugtraq)
  • Re: dynamic vs. static: the age-old debate
    ... int addx+y; ... orthogonal to static/dynamic/soft typing. ... $ dynamic-compiler program.source -o program ... will overflow, leaving the "overflow behavior is undefined" ...
    (comp.lang.misc)