Skip to content

Commit

Permalink
Clarify null termination requirements of SourceMapParser
Browse files Browse the repository at this point in the history
Summary:
Clarify that the SourceMapParser must always receive a null-terminated
buffer. Remove the `StringRef` overload since accessing past the end of
a `StringRef` is unusual.

Reviewed By: avp

Differential Revision: D68963920

fbshipit-source-id: 69ac05f5b8831e39753124493d939648a28e1299
  • Loading branch information
neildhar authored and facebook-github-bot committed Jan 31, 2025
1 parent 4af630f commit 65de355
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
14 changes: 2 additions & 12 deletions include/hermes/SourceMap/SourceMapParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,14 @@ namespace hermes {
/// parses.
class SourceMapParser {
public:
/// Parse input \p sourceMap and return parsed SourceMap.
/// Parse input \p sourceMap and return parsed SourceMap. \p sourceMap must
/// have a past-the-end null terminator.
/// On failure if malformed, prints an error message and returns nullptr.
static std::unique_ptr<SourceMap> parse(
llvh::MemoryBufferRef sourceMap,
llvh::StringRef baseDir,
SourceErrorManager &sm);

/// Parse input \p sourceMapContent and return parsed SourceMap.
/// Set the filename of the map file to "<source map>".
/// On failure if malformed, prints an error message and returns nullptr.
static std::unique_ptr<SourceMap> parse(
llvh::StringRef sourceMapContent,
llvh::StringRef baseDir,
SourceErrorManager &sm) {
return parse(
llvh::MemoryBufferRef(sourceMapContent, "<source map>"), baseDir, sm);
}

private:
SourceMapParser() = delete;
SourceMapParser(SourceMapParser &) = delete;
Expand Down
4 changes: 2 additions & 2 deletions lib/SourceMap/c-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ extern "C" HermesSourceMap *hermes_source_map_parse(
SourceErrorManager sm;
SimpleDiagHandlerRAII handler(sm);

auto sourceMap =
SourceMapParser::parse(llvh::StringRef(source, len - 1), {}, sm);
llvh::MemoryBufferRef mbref(llvh::StringRef{source, len - 1}, "<source map>");
auto sourceMap = SourceMapParser::parse(mbref, {}, sm);
// Defensive programming.
if (!sourceMap && !handler.haveErrors())
sm.error(SMLoc{}, "internal error");
Expand Down
3 changes: 2 additions & 1 deletion unittests/VMRuntime/StackTracesTreeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ struct StackTracesTreeTest : public RuntimeTestFixtureBase {
llvh::raw_string_ostream resStream(res);
SourceErrorManager sm;
sourceMapGen.outputAsJSON(resStream);
auto sourceMap = SourceMapParser::parse(res, /* baseDir */ "", sm);
llvh::MemoryBufferRef smBuf(res, "");
auto sourceMap = SourceMapParser::parse(smBuf, /* baseDir */ "", sm);
assert(
sm.getErrorCount() == 0 && "source map generation or parsing failed");
return sourceMap;
Expand Down

0 comments on commit 65de355

Please sign in to comment.