News for hackers. Stuff that matters.
Re:How to dismantle a compiler bomb by nur0n on Friday March 09, 2018 @04:58PM (#16555391)
The main practical takeaway is to prefer iterators over pre-generated arrays.

Also, TIL according to the C standard, 'main' is not a reserved identifier! (https://stackoverflow.com/questions/34764796/why-does-declar...)

If anyone can clarify: I assumed that the gcc read literals directly into their 'target' type, but it seems like some literals (such as '-1u') are read as signed integers first then typecasted to the the target type?

Re:How to dismantle a compiler bomb by shakna on Friday March 09, 2018 @06:16PM (#16555922)
> Also, TIL according to the C standard, 'main' is not a reserved identifier! (https://stackoverflow.com/questions/34764796/why-does-declar...)

That's not quite true.

Main is not required to be defined in a freestanding environment, but in a hosted environment:

> 5.1.2.2.1 Program startup

> 1

> The function called at program startup is named

> main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

    int main(void) { /* ... */ }
> or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

    int main(int argc, char *argv[]) { /* ... */ }
> or equivalent; 10) or in some other implementation-defined manner.

main can take on differing types, but it then becomes undefined-behaviour, which allows the compiler to do whatever it wants.

(5.1.2.2.1 Program startup, C11 Standard http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)

Re:How to dismantle a compiler bomb by derefr on Friday March 09, 2018 @06:31PM (#16556031)
> according to the C standard, 'main' is not a reserved identifier!

In fact, main() is just a convention of libc. You can have C without libc. (Such as when writing a kernel!)

Now, attempting to link a standalone executable without a '_start' symbol, on the other hand...

Re:How to dismantle a compiler bomb by shakna on Friday March 09, 2018 @07:35PM (#16556334)
> In fact, main() is just a convention of libc.

No, it's not just a convention. 'main' is defined as the execution entrypoint in at least the C11 [0], C90 [1] standards. Both have both these forms defined:

    int main(void) {}

    int main(int argc, char* argv[]) {}
You don't have to follow that convention, but then it becomes implementation-defined behaviour.

C without libc can still expect to have a main. One doesn't imply the other. It's just without a main, you also have to manually link to _start as well.

[0] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

[1] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

Re:How to dismantle a compiler bomb by prattmic on Saturday March 10, 2018 @12:58PM (#16559422)
> Now, attempting to link a standalone executable without a '_start' symbol, on the other hand...

GNU ld and gold both support --entry to change the entry point. Alternatively, you can specify it in your linker script.

https://gist.github.com/prattmic/1f3618025aab52b0e90e88445b6...

    $ gcc -nostdlib -ffreestanding -Wl,--entry=foobar -o main main.S
    $ ./main; echo $?
    42