← Back to context

Comment by dataflow

1 year ago

> That said, I don’t see why it wouldn’t be possible to cram in 24 bytes of null-terminated payload

Note that I didn't say this is impossible, just that the given trick wouldn't work.

However, this is impossible for general strings. The only way could possibly make this work is if you constrain the inline string somehow (e.g., to UTF-8), so that some shorter strings failing that constraint are forced to go on the heap too. Otherwise you have 1 fixed zero byte at the end, and 23 fully flexible bytes, leaving you no way to represent an out-of-line string.

(Well, you could do it if you use the address as a key into some static map or such where you shove the real data, but that's cheating and beside the point here.)

Think that through again. An inline string needs a fixed 0 byte at the end. A heap string does not. Therefore if the last byte is anything other than 0 you have a heap string.

Inline strings only use 0.4% of your possible values.

  • Oof, you're right, thank you. In my mind the last byte was obviously zero for a heap string too, since the pointer or sizes would've had a zero upper byte. Somehow I never accounted for the fact that on 64-bit there's no need to represent it that way. Fantastic point!