Skip to content

Commit

Permalink
fix: crash in comment parsing (#1890)
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-paris authored Aug 21, 2023
1 parent f2a8620 commit eaf9f0a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@ function tokenize(source, alternateCommentMode) {

// see if remaining line matches comment pattern
var lineText = source.substring(startOffset, endOffset);
// look for 1 or 2 slashes since startOffset would already point past
// the first slash that started the comment.
var isComment = /^\s*\/{1,2}/.test(lineText);
var isComment = /^\s*\/\//.test(lineText);
return isComment;
}

Expand Down Expand Up @@ -268,7 +266,7 @@ function tokenize(source, alternateCommentMode) {
// check for double-slash comments, consolidating consecutive lines
start = offset;
isDoc = false;
if (isDoubleSlashCommentLine(offset)) {
if (isDoubleSlashCommentLine(offset - 1)) {
isDoc = true;
do {
offset = findEndOfLine(offset);
Expand Down
21 changes: 21 additions & 0 deletions tests/data/comments-alternate-parse.proto
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ enum Test3 {
FIVE = 5; // Trailing comment for value with both types of comments after field with trailing comment.
}

// Single line comment,
/*
followed by a block comment, followed by a newline. (issue #1616)
*/
message Test4 {
}

// line comment with
// whitespace in front
message Test5 {
}

// Multiple
// different
// comments
/*
with strange whitespace
*/
message Test6 {
}

service ServiceTest {
// My method does things
rpc SingleLineMethod (MyRequest) returns (MyResponse);
Expand Down
5 changes: 4 additions & 1 deletion tests/docs_comments_alternate_parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var tape = require("tape");
var protobuf = require("..");

tape.test("proto comments in alternate-parse mode", function(test) {
test.plan(24);
test.plan(27);
var options = {alternateCommentMode: true};
var root = new protobuf.Root();
root.load("tests/data/comments-alternate-parse.proto", options, function(err, root) {
Expand All @@ -13,6 +13,9 @@ tape.test("proto comments in alternate-parse mode", function(test) {
test.equal(root.lookup("Test1").comment, "Message with\na\nmulti-line comment.", "should parse double-slash multiline comment");
test.equal(root.lookup("Test2").comment, "Message\nwith\na multiline plain slash-star\ncomment.", "should parse slash-star multiline comment");
test.equal(root.lookup("Test3").comment, "Message\nwith\na\ncomment and stars.", "should parse doc-block multiline comment");
test.equal(root.lookup("Test4").comment, "followed by a block comment, followed by a newline. (issue #1616)", "should parse double slash comment followed by block comment");
test.equal(root.lookup("Test5").comment, "line comment with\nwhitespace in front", "should ignore leading whitespace when parsing line comments");
test.equal(root.lookup("Test6").comment, "with strange whitespace", "should parse block comment preceeded by double slash comments with leading whitespace");

test.equal(root.lookup("Test1.field1").comment, "Field with a doc-block comment.", "should parse doc-block field comment");
test.equal(root.lookup("Test1.field2").comment, "Field with a single-line comment starting with two slashes.", "should parse double-slash field comment");
Expand Down

0 comments on commit eaf9f0a

Please sign in to comment.