src_provide_workaround_for_container-overflow.patch 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Daniel Lemire <[email protected]>
  3. Date: Tue, 29 Oct 2024 12:28:47 -0400
  4. Subject: src: provide workaround for container-overflow
  5. Address a sanitizer error 'container-overflow', which happens only on systems where the
  6. standard library has been annotated to warn about reads between the std::string's end
  7. and the end of the its allocated memory (std::string:capacity). This reads are memory safe
  8. but they can also be a sign that there is a real bug. In the instance of issue 55584,
  9. it is not a bug, it is a false positive.
  10. In some instances, it is possible to indicate to the compiler that we want to disallow
  11. these checks to avoid the false positive, but I could not find documentation on this topic
  12. In a future release of simdjson, we will provide a more convenient function that
  13. avoids such ugly workaround.
  14. diff --git a/src/node_modules.cc b/src/node_modules.cc
  15. index dfd115a9eccc6b58d63a72ac450a1497354482dd..16a9f923148835daa95d3578e5941b284ff71434 100644
  16. --- a/src/node_modules.cc
  17. +++ b/src/node_modules.cc
  18. @@ -100,11 +100,23 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON(
  19. if (ReadFileSync(&package_config.raw_json, path.data()) < 0) {
  20. return nullptr;
  21. }
  22. + // In some systems, std::string is annotated to generate an
  23. + // AddressSanitizer: container-overflow error when reading beyond the end of
  24. + // the string even when we are still within the capacity of the string.
  25. + // https://github.com/google/sanitizers/wiki/AddressSanitizerContainerOverflow
  26. + // https://github.com/nodejs/node/issues/55584
  27. + // The next lines are a workaround to avoid this false positive.
  28. + size_t json_length = package_config.raw_json.size();
  29. + package_config.raw_json.append(simdjson::SIMDJSON_PADDING, ' ');
  30. + simdjson::padded_string_view json_view(package_config.raw_json.data(),
  31. + json_length,
  32. + package_config.raw_json.size());
  33. + // End of workaround
  34. simdjson::ondemand::document document;
  35. simdjson::ondemand::object main_object;
  36. simdjson::error_code error =
  37. - binding_data->json_parser.iterate(package_config.raw_json).get(document);
  38. + binding_data->json_parser.iterate(json_view).get(document);
  39. const auto throw_invalid_package_config = [error_context, path, realm]() {
  40. if (error_context == nullptr) {