fuzz_binary_decode.cc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include <ua_types.h>
  5. #include "ua_server_internal.h"
  6. #include "ua_config_default.h"
  7. #include "ua_log_stdout.h"
  8. #include "ua_types_encoding_binary.h"
  9. /*
  10. ** Main entry point. The fuzzer invokes this function with each
  11. ** fuzzed input.
  12. */
  13. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  14. if (size == 0)
  15. return 0;
  16. const uint8_t *ptr = data;
  17. size_t ptrSize = size;
  18. // get some random type
  19. uint16_t typeIndex = ptr[0];
  20. ptr++;
  21. ptrSize--;
  22. if (typeIndex >= UA_TYPES_COUNT)
  23. return 0;
  24. size_t offset = 0;
  25. if (ptrSize >= sizeof(size_t)) {
  26. offset = (*ptr);
  27. ptr += sizeof(size_t);
  28. ptrSize -= sizeof(size_t);
  29. }
  30. void *dst = UA_new(&UA_TYPES[typeIndex]);
  31. const UA_ByteString binary = {
  32. ptrSize, //length
  33. (UA_Byte *)(void *)ptr //data
  34. };
  35. UA_StatusCode ret = UA_decodeBinary(&binary, &offset, dst, &UA_TYPES[typeIndex], 0, nullptr);
  36. if (ret == UA_STATUSCODE_GOOD) {
  37. //do nothing
  38. }
  39. UA_delete(dst, &UA_TYPES[typeIndex]);
  40. return 0;
  41. }