Live data from Hacker News

*(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

youtube.com

71–80 of 199 posts

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#71
post #55

Earlier quoted context omitted.

I'd agree with that, to the extent that understanding how badly your tools can hurt you is an important thing to learn. Consider how a surgeon would respond if told not to use a scalpel because of the risk of accidental injury when using a sharp tool. We learn from our mistakes - to which I'd add, sometimes we can afford to make mistakes (home programs) and other times we can't (safety-critical code).

I think a more accurate analogy would be some kind of scalpel that would not let the surgeon cut deeper than they needed. So if they needed a 1 cm incision, it would somehow stop the blade form going in 1.1 cm. In that case, some experienced surgeon may say "But I know how to make a 1cm incision!", but I think that the reality is that preventing a mistake from happening is valuable. You see the same arguments against…

https://www.sawstop.com

This is my favorite one

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#73

This statement would be technically legal on its own in x86 real mode if the compiler didn't do null pointer checks. However it would set the divide-by-zero IRQ handler to itself 0000:0000, and when the next division by zero happened, the machine run into UB (likely a reset or halt) because it would jump there, do 4x ADD byte ptr [BX + SI], AL (or ADD byte ptr [EAX], AL) followed by running the remaining interrupt ve…

Good thing this was covered in the talk

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#74
post #42

Earlier quoted context omitted.

You say that as if that's a bad thing – I absolutely need to kept safe from myself, because I need all the help I need to make sure I'm doing things right!

use a malloc that has the extra saftey features, run under an interpreted C, and several other ways to handle it (sandbox program, emulator, etc). Changing the underlying C language should NOT be one of them.

The only way to make C "safe" without modifying the language is to run it in a VM, and even that doesn't fully encompass what people mean by safety.

It's an old language. Some of the fundamental mistakes were just things that were common at the time and only look so bad in hindsight (e.g. strings, locales, nullable pointers, half the k&r standard library). That doesn't mean we can't and shouldn't do better where we can though.

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#75
post #27

Earlier quoted context omitted.

Find me one contemporary example (ANSI C) with a disassembled screenshot. This is writing sizeof(char) (== 1 almost everywhere) zero to address zero. It is not using a NULL macro or other predefined symbol. In the real world, this would generally write a byte to address 0000:0000, leading to UB because it would fuck up the divide-by-zero IV. PS: I used Borland C++ 3.1, Microsoft C++ 3.x and 4.5x, Watcom, and early GN…

It doesn't have to be the NULL macro, which is correctly defined as plain 0. The literal 0 is treated specially, so this could indeed be one of those 'turns into a weird bit pattern NULL pointers', if such a thing existed in the wild anymore. But you're correct in that there probably haven't been any since the turn of the century or whenever the last Univac mainframes got turned off.

Apparently according to the c-faqs link elsethread

    execl takes a variable-length, null-pointer-terminated list of character pointer arguments, and is correctly called like this:
    execl("/bin/sh", "sh", "-c", "date", (char *)0);
Due to ececl being a variadic function it can not take advantage of a prototype to instruct the compiler that one of its arguments needs to be treated as a pointer context.

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#76

Earlier quoted context omitted.

Find me one contemporary example (ANSI C) with a disassembled screenshot. This is writing sizeof(char) (== 1 almost everywhere) zero to address zero. It is not using a NULL macro or other predefined symbol. In the real world, this would generally write a byte to address 0000:0000, leading to UB because it would fuck up the divide-by-zero IV. PS: I used Borland C++ 3.1, Microsoft C++ 3.x and 4.5x, Watcom, and early GN…

> sizeof(char) (== 1 almost everywhere) sizeof char is 1 by definition everywhere. /pedantic

> sizeof char is 1 by definition everywhere.

Parentheses are required around char because it's a type.

/pedantic

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#77
post #25

When I see that code my head immediately translates to "store a byte with value 0 at memory address zero". 0 the integer needs to be cast to a char * to treat it as a byte address (rather than a word) and the * means "assign to the memory location given by the pointer. I would probably use something like this when hacking on an Apple II with a zero page. I have also written programs that ran on VMS which dumped memor…

yes, compiler will align integers on word boundaries. casting to char, just uses the first 8 bits of integer word. Char was originaly just sematic macro for integer. pointer to char is 8 sequenial bits somewhere in the group of integer spaced bits. if you're lucky, the 8 bits are at the start of the integer group of bits, otherwise, segfault. Originaly using anonymous unions & placing largest bit count variable as fi…

I see you've been lucky enough to only work on systems where CHAR_BIT=8. It's not an alignment issue because there may not be a distinct pointer representation for the other bits, rather than alignment issues where a pointer representation exists even if you can't use it.

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#78

Old school comment... back in the day, there were computers on which dereferencing the null pointer would in fact return zero. And given that a typical linked list is terminated by a null, mistakes could creep in where "points to a 0" was checked when "pointer is 0" should have been. Needless to say, code like this would crash in mysterious ways when compiled on a machine where location 0, while readable, had nonzero…

Many(most?) Cortex-M4 systems boot from the reset vector and stack pointer stored in the first 8B of memory, so it's a valid address on a ton of small systems today.

You can relocate the vector table and to block it off with the MPU if you have one and memory to spare.

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#80
post #69

fun trivia: in WebAssembly it's valid to read/write to memory address 0.

I’m glad you appreciated me saying exactly this in the talk

Ah fuck, I grepped the YouTube transcript for WASM/WebAssembly but I guess youtube's automatic captions aren't very good. I feel silly now for not watching the video before commenting.
Post reply on HN