fuzz_src_ua_util.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. *
  5. * Copyright 2019 (c) fortiss (Author: Stefan Profanter)
  6. */
  7. #include "custom_memory_manager.h"
  8. #include <open62541/util.h>
  9. static int tortureParseEndpointUrl(const uint8_t *data, size_t size) {
  10. const UA_String endpointUrl = {
  11. size, (UA_Byte* )(void*)data
  12. };
  13. UA_String hostname;
  14. UA_UInt16 port;
  15. UA_String path;
  16. UA_parseEndpointUrl(&endpointUrl, &hostname, &port, &path);
  17. return 0;
  18. }
  19. static int tortureParseEndpointUrlEthernet(const uint8_t *data, size_t size) {
  20. const UA_String endpointUrl = {
  21. size, (UA_Byte* )(void*)data
  22. };
  23. UA_String target;
  24. UA_UInt16 vid;
  25. UA_Byte prid;
  26. UA_parseEndpointUrlEthernet(&endpointUrl, &target, &vid, &prid);
  27. return 0;
  28. }
  29. /*
  30. ** Main entry point. The fuzzer invokes this function with each
  31. ** fuzzed input.
  32. */
  33. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  34. if (!UA_memoryManager_setLimitFromLast4Bytes(data, size))
  35. return 0;
  36. size -= 4;
  37. if (size == 0)
  38. return 0;
  39. // use first byte to decide which function should be fuzzed
  40. const uint8_t select = data[0];
  41. const uint8_t *newData = &data[1];
  42. size_t newSize = size-1;
  43. switch(select) {
  44. case 0:
  45. return tortureParseEndpointUrl(newData, newSize);
  46. case 1:
  47. return tortureParseEndpointUrlEthernet(newData, newSize);
  48. default:
  49. return 0;
  50. }
  51. }