TheAlgorithms / Zig

Collection of Algorithms implemented in Zig.
https://the-algorithms.com/language/zig
MIT License
142 stars 27 forks source link

Don't use undefined value in build.zig #3

Closed InKryption closed 1 year ago

InKryption commented 1 year ago

Using undefined here can trigger UB - instead return early.

AndersonTorres commented 1 year ago

What is UB?

InKryption commented 1 year ago

It stands for Undefined Behavior. In zig, assigning the undefined literal is equivalent to not initializing whatever you're assigning it to, meaning it probably has some meaningless garbage value - except in debug mode, where it assigns screaming bytes (0xAA) as a debugging convenience.

In this case, when you failed to pass -Dalgorithm=<algo>, it caused the length of the slice to almost never match against that of any of the strings that were compared against, which meant it wouldn't try to access the garbage pointer location, which would have caused a segfault otherwise.

TLDR: you shouldn't read values that haven't been initialized, which is what undefined here signifies.