sophiajt / june

MIT License
813 stars 31 forks source link

raw buffer initialization of `bool` has guaranteed UB because of va_args usage in generated code #45

Open lefticus opened 3 months ago

lefticus commented 3 months ago

I believe you are getting a 4 byte write (default int, 32bits on all modern platforms) for each 1 byte bool object.

Input

mut blob = raw[true, false]

Output from GCC (with no compiler warnings enabled)

build/debug/main.c: In function ‘create_buffer_11’:
build/debug/main.c:402:17: warning: ‘_Bool’ is promoted to ‘int’ when passed through ‘...’
  402 | *(output + i) = va_arg(args, bool);
      |                 ^~~~~~
build/debug/main.c:402:17: note: (so you should pass ‘int’ not ‘_Bool’ to ‘va_arg’)
build/debug/main.c:402:17: note: if this code is reached, the program will abort

Output from clang:

*(output + i) = va_arg(args, bool);
                             ^~~~
/usr/lib/clang/16/include/stdbool.h:20:14: note: expanded from macro 'bool'
#define bool _Bool
             ^~~~~
/usr/lib/clang/16/include/stdarg.h:36:50: note: expanded from macro 'va_arg'
#define va_arg(ap, type)    __builtin_va_arg(ap, type)
                                                 ^~~~
1 warning generated.