fork(1) download
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <fenv.h>
  6.  
  7. struct somestruct
  8. {
  9. int foo;
  10. float bar;
  11. };
  12.  
  13. int main(void) {
  14. // Симулируем машинку, которая может в sNaN.
  15. feenableexcept(FE_INVALID);
  16.  
  17. // Симулируем нехороший пакет с данными.
  18. static const uint8_t bad_input[] = {
  19. 0xad, 0xde, 0xad, 0xde, 0xff, 0xff, 0xbf, 0xff,
  20. };
  21.  
  22. // Не подозревая о подвохе, декодируем пакет.
  23. struct somestruct s;
  24. memcpy(&s, bad_input, sizeof(bad_input));
  25.  
  26. printf("Successfully parsed `somestruct' from stream.\n");
  27. fflush(stdout);
  28.  
  29. s.bar += 0.0f;
  30.  
  31. printf("foo = %x, bar = %f\n", s.foo, s.bar);
  32. fflush(stdout);
  33. }
  34.  
Runtime error #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
Successfully parsed `somestruct' from stream.