test_opus_padding.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright (c) 2012 Xiph.Org Foundation
  2. Written by Jüri Aedla and Ralph Giles */
  3. /*
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  16. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /* Check for overflow in reading the padding length.
  25. * http://lists.xiph.org/pipermail/opus/2012-November/001834.html
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "opus.h"
  31. #include "test_opus_common.h"
  32. #define PACKETSIZE 16909318
  33. #define CHANNELS 2
  34. #define FRAMESIZE 5760
  35. int test_overflow(void)
  36. {
  37. OpusDecoder *decoder;
  38. int result;
  39. int error;
  40. unsigned char *in = malloc(PACKETSIZE);
  41. opus_int16 *out = malloc(FRAMESIZE*CHANNELS*sizeof(*out));
  42. fprintf(stderr, " Checking for padding overflow... ");
  43. if (!in || !out) {
  44. fprintf(stderr, "FAIL (out of memory)\n");
  45. return -1;
  46. }
  47. in[0] = 0xff;
  48. in[1] = 0x41;
  49. memset(in + 2, 0xff, PACKETSIZE - 3);
  50. in[PACKETSIZE-1] = 0x0b;
  51. decoder = opus_decoder_create(48000, CHANNELS, &error);
  52. result = opus_decode(decoder, in, PACKETSIZE, out, FRAMESIZE, 0);
  53. opus_decoder_destroy(decoder);
  54. free(in);
  55. free(out);
  56. if (result != OPUS_INVALID_PACKET) {
  57. fprintf(stderr, "FAIL!\n");
  58. test_failed();
  59. }
  60. fprintf(stderr, "OK.\n");
  61. return 1;
  62. }
  63. int main(void)
  64. {
  65. const char *oversion;
  66. int tests = 0;;
  67. iseed = 0;
  68. oversion = opus_get_version_string();
  69. if (!oversion) test_failed();
  70. fprintf(stderr, "Testing %s padding.\n", oversion);
  71. tests += test_overflow();
  72. fprintf(stderr, "All padding tests passed.\n");
  73. return 0;
  74. }