Considering how much of Andrew’s code I’ve seen, bugs are abnormally rare. Well… that makes today special…I actually found one! It’s in an example he wrote in his pulseaudio fork (very mission-critical code, of course). So here’s your chance to prove your intellect: can you spot the bug?
P.S. no cheating and looking at the PR queue where I explain it
Ah ok, ptr[0..write_len] with a i32 pointer when it should be a byte pointer - arguably also a failure of the Pulse Audio C API though (void**)
PS: …I didn’t cheat, but after finding the problem I wanted to check if ChatGPT does too, and it did. Might be the one thing where LLMs can actually be useful
Interesting, I asked it a slightly different question (smth along the line of "can you find the bug in this code snippet"), and it only isolated the 'real' bug. Then I was just asking smth like "find the bug" and the output was nearly identical to your's with lots of noise. ...not really surprising of course... my prediction is that the next big thing in AI will be specialized "prompt languages" which translate some SQL-like syntax back into just the right human language patterns to produce useful results ;)
...the explanation given is still slightly wrong lol (not writing details here to not spoil the original question - but it mixes up C semantics with Zig semantics for a specific function)
to be clear in my example I asked it to find any bug, but I provided it with code that fixed the original issue. for the tool to be useful it needs to identify bugs when present, but also not hallucinate bugs when there are none
I am a weak mind and just looked at the spoiler, but I want to mention that we follow pretty strict naming convention at TigerBeetle to minimize this sort of thing.
We never use len / length. What we use are count/index and size/offset.
size is always size in bytes. size == count * @sizeOf(T)
count is always a 1-based logical number of items
index is always a 0-based index of logical item. index goes with count and index + 1 == count
offset is always a 0-based byte offset. offset goes with size and offset + 1 == size
Use explicitly sized types: Use data types with explicit sizes, like u32 or i64, instead of architecture-dependent types like usize. This keeps behavior consistent across platforms and avoids size-related errors, improving portability and reliability.
Just want to add the caveat that count is often used when traversing the collection, counting the elements. So is not a trivial operation. But if it is trivially known, usually as a field, then len/length is used instead.
I’m a noob, what’s the meaning/significance of “with a i32 pointer when it should be a byte pointer”?
Indexing is just a little math: ptr + (index * element_size) (element_size is in bytes) A byte is obviously 1 byte big, but an i32 is 4 bytes big. So indexing an i32 ptr will go 4 times the distance in memory compared to a byte ptr.
"Might be the one thing where LLMs can actually be useful" eh... not much https://chatgpt.com/share/6895bbc1-4818-800a-9a40-d74407aa8761 (spoilers)
– kristoffInteresting, I asked it a slightly different question (smth along the line of "can you find the bug in this code snippet"), and it only isolated the 'real' bug. Then I was just asking smth like "find the bug" and the output was nearly identical to your's with lots of noise. ...not really surprising of course... my prediction is that the next big thing in AI will be specialized "prompt languages" which translate some SQL-like syntax back into just the right human language patterns to produce useful results ;)
– flooohPS: (spoiler!) https://chatgpt.com/share/6895c1c8-a6ec-800d-a8a6-10406097bd7d ...for some random reason I was asking just the right question, do any variation of the question and it becomes noise.
– floooh...the explanation given is still slightly wrong lol (not writing details here to not spoil the original question - but it mixes up C semantics with Zig semantics for a specific function)
– flooohto be clear in my example I asked it to find any bug, but I provided it with code that fixed the original issue. for the tool to be useful it needs to identify bugs when present, but also not hallucinate bugs when there are none
– kristoff