Hacker News new | past | comments | ask | show | jobs | submit login

I believe this[1] is the patch that fixes this bug. I tried to reproduce the behavior but couldn't succeed. Maybe I was doing something wrong. Would really appreciate it if someone here could show a test case where this matters.

[1] https://github.com/LoupVaillant/Monocypher/commit/347189c50c...




Your failure to reproduce the behaviour is normal: on the compilers I have tested, this doesn't affect the generated binary.

To see the difference, you need to first modify the makefile to use UBSan instead of just GCC (just comment the active CC line, and uncomment the right one). You may want to tone down the optimisations for faster compilation as well. Then you can run `make clean` and `./test.sh`. You should have a warning on commits prior to this patch.


Compile this program with -fsanitize=undefined:

  #include <stddef.h>
  #include <inttypes.h>
  
  int main(int argc, char **argv)
  {
  	uint8_t msg[] = { 255 };
  	size_t i = 0;
  	size_t c_index = 255;
  	return msg[i] << ((c_index % 4) * 8);
  }
You get:

  runtime error: left shift of 255 by 24 places cannot be represented in type 'int'


`ctx->c` is a u32[1] and msg is u8. So yeah there is integer promotion here.

[1]: https://github.com/LoupVaillant/Monocypher/blob/master/src/m...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: