fuzz-xml.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (C) 2023 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <stdio.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <pjlib.h>
  22. #include <pjlib-util.h>
  23. #define kMinInputLength 10
  24. #define kMaxInputLength 5120
  25. pj_pool_factory *mem;
  26. int XML_parse(uint8_t *data, size_t Size) {
  27. pj_pool_t *pool;
  28. pj_xml_node *root;
  29. char *output;
  30. size_t output_size;
  31. pool = pj_pool_create(mem, "xml", 4096, 1024, NULL);
  32. root = pj_xml_parse(pool, (char *)data, Size);
  33. if (!root) {
  34. goto on_error;
  35. }
  36. output = (char*)pj_pool_zalloc(pool, Size + 512);
  37. output_size = pj_xml_print(root, output, Size + 512, PJ_TRUE);
  38. if (output_size < 1) {
  39. goto on_error;
  40. }
  41. pj_pool_release(pool);
  42. return 0;
  43. on_error:
  44. pj_pool_release(pool);
  45. return 1;
  46. }
  47. extern int
  48. LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
  49. {
  50. if (Size < kMinInputLength || Size > kMaxInputLength) {
  51. return 1;
  52. }
  53. int ret = 0;
  54. uint8_t *data;
  55. pj_caching_pool caching_pool;
  56. /* Add NULL byte */
  57. data = (uint8_t *)calloc((Size+1), sizeof(uint8_t));
  58. memcpy((void *)data, (void *)Data, Size);
  59. /* init Calls */
  60. pj_init();
  61. pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0);
  62. pj_log_set_level(0);
  63. mem = &caching_pool.factory;
  64. /*Calls fuzzer*/
  65. ret = XML_parse(data, Size);
  66. free(data);
  67. pj_caching_pool_destroy(&caching_pool);
  68. return ret;
  69. }