>>1
To break this code I'd have to input a number bigger than INT_MAX or smaller than INT_MIN or hit EOF or have their sum being bigger than INT_MAX or smaller than INT_MIN.
All those scenarios invoke undefined behavior.
>>1
The main declaration is invalid C99 code.
The printf statements might display after scanf has taken input due to line buffering.
And of course there is the complete lack of error checking.
Can't be hardened without rewriting it completely.
scanf() is a fucking black hole.
Name:
Anonymous2008-10-23 6:13
>>9
the C99 standard specifies that int main(void) { /* ... */ }, int main(int argc, char *argv[]) { /* ... */ }, or equivalent are valid in 5.1.2.2.1.
in 6.7.5.3 it specifies: The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.
and An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters.
so int main(void) { /* ... */ } and int main() { /* ... */ } are equivalent.
>>9 C99 does allow omitting arguments >>10 gcc's -pedantic is not a magic option to find all incompatibilities with C99, program may be invalid even if gcc doesn't produce any warnings (I read about it some time ago on THE INTERNET)
POSIX only specifies that LINE_MAX must be at least 2048: http://www.opengroup.org/onlinepubs/000095399/basedefs/limits.h.html {LINE_MAX}
Unless otherwise noted, the maximum length, in bytes, of a utility's input line (either standard input or another file), when the utility is described as processing text files. The length includes room for the trailing <newline>.
Minimum Acceptable Value: {_POSIX2_LINE_MAX} {_POSIX2_LINE_MAX}
Unless otherwise noted, the maximum length, in bytes, of a utility's input line (either standard input or another file), when the utility is described as processing text files. The length includes room for the trailing <newline>.
Value: 2 048
defining LINE_MAX equal to INT_MAX is stupid and would break a lot of existing code: http://www.google.com/codesearch?as_q=char\s%2B\w%2B[LINE_MAX\]%3B&btnG=Search+Code&as_lang=c&as_case=y Results 1 - 10 of about 2,000. (0.05 seconds)
>>32 defining LINE_MAX equal to INT_MAX is stupid and would break a lot of existing code:
That existing code is broken anyway if it assumes that LINE_MAX is going to be much less than INT_MAX.
POSIX says nothing about the maximum value of LINE_MAX; in fact all you're guaranteed is that it's going to be >= 2048.
POSIX says nothing about the maximum value of LINE_MAX; in fact all you're guaranteed is that it's going to be >= 2048.
well, there is this: http://www.opengroup.org/onlinepubs/000095399/utilities/tail.html Tails relative to the end of the file may be saved in an internal buffer, and thus may be limited in length. Such a buffer, if any, shall be no smaller than {LINE_MAX}*10 bytes.
so if tail uses an internal buffer for tails relative to the end of a file, LINE_MAX can't possibly be more than UINTPTR_MAX / 10.
is there any way to implement tail without using an internal buffer for tails relative to the end of a file?
If the file is a regular file, just seek to the end and read backwards until you're at the point where you need to start tailing, if it's a file of possibly unbounded length then you only need to keep the last n lines/bytes in some sort of dynamically allocated expanding ring buffer (like a FIFO). Of course, positions relative to the beginning of the file are trivial: just keep consuming input (unseekable) until the position is reached, or seek (bounded, seekable) to the position.
...I should probably go write anoncoreutils tail now.
if it's a file of possibly unbounded length then you only need to keep the last n lines/bytes in some sort of dynamically allocated expanding ring buffer (like a FIFO).
Name:
Anonymous2008-10-24 2:12
What the fuck /prog/??
You should write your test cases before you write even a single line of production code!!
Name:
Anonymous2008-10-24 3:26
>>40
Fuck your test cases and enterprise best practices.
Name:
Anonymous2008-10-24 6:20
What the fuck /prog/??
You should prove your programs correct before you write even a single line of production code!!
Name:
Anonymous2008-10-24 14:15
I have a scheme macro that automatically creates all possible test cases for me. Unfortunately I created it at work and I signed a non-disclosure agreement with my boss, so I can't share it with the world forever.