test_opus_padding.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Check for overflow in reading the padding length.
  2. * Example by Jüri Aedla and Ralph Giles
  3. * http://lists.xiph.org/pipermail/opus/2012-November/001834.html
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "opus.h"
  9. #include "test_opus_common.h"
  10. #define PACKETSIZE 16909318
  11. #define CHANNELS 2
  12. #define FRAMESIZE 5760
  13. int test_overflow(void)
  14. {
  15. OpusDecoder *decoder;
  16. int result;
  17. int error;
  18. unsigned char *in = malloc(PACKETSIZE);
  19. opus_int16 *out = malloc(FRAMESIZE*CHANNELS*sizeof(*out));
  20. fprintf(stderr, " Checking for padding overflow... ");
  21. if (!in || !out) {
  22. fprintf(stderr, "FAIL (out of memory)\n");
  23. return -1;
  24. }
  25. in[0] = 0xff;
  26. in[1] = 0x41;
  27. memset(in + 2, 0xff, PACKETSIZE - 3);
  28. in[PACKETSIZE-1] = 0x0b;
  29. decoder = opus_decoder_create(48000, CHANNELS, &error);
  30. result = opus_decode(decoder, in, PACKETSIZE, out, FRAMESIZE, 0);
  31. opus_decoder_destroy(decoder);
  32. free(in);
  33. free(out);
  34. if (result != OPUS_INVALID_PACKET) {
  35. fprintf(stderr, "FAIL!\n");
  36. test_failed();
  37. }
  38. fprintf(stderr, "OK.\n");
  39. return 1;
  40. }
  41. int main(void)
  42. {
  43. const char *oversion;
  44. int tests = 0;;
  45. iseed = 0;
  46. oversion = opus_get_version_string();
  47. if (!oversion) test_failed();
  48. fprintf(stderr, "Testing %s padding.\n", oversion);
  49. tests += test_overflow();
  50. fprintf(stderr, "All padding tests passed.\n");
  51. return 0;
  52. }