Skip to content

Commit eaf9f0a

Browse files
authored
fix: crash in comment parsing (#1890)
1 parent f2a8620 commit eaf9f0a

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/tokenize.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,7 @@ function tokenize(source, alternateCommentMode) {
197197

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

@@ -268,7 +266,7 @@ function tokenize(source, alternateCommentMode) {
268266
// check for double-slash comments, consolidating consecutive lines
269267
start = offset;
270268
isDoc = false;
271-
if (isDoubleSlashCommentLine(offset)) {
269+
if (isDoubleSlashCommentLine(offset - 1)) {
272270
isDoc = true;
273271
do {
274272
offset = findEndOfLine(offset);

tests/data/comments-alternate-parse.proto

+21
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ enum Test3 {
7979
FIVE = 5; // Trailing comment for value with both types of comments after field with trailing comment.
8080
}
8181

82+
// Single line comment,
83+
/*
84+
followed by a block comment, followed by a newline. (issue #1616)
85+
*/
86+
message Test4 {
87+
}
88+
89+
// line comment with
90+
// whitespace in front
91+
message Test5 {
92+
}
93+
94+
// Multiple
95+
// different
96+
// comments
97+
/*
98+
with strange whitespace
99+
*/
100+
message Test6 {
101+
}
102+
82103
service ServiceTest {
83104
// My method does things
84105
rpc SingleLineMethod (MyRequest) returns (MyResponse);

tests/docs_comments_alternate_parse.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var tape = require("tape");
33
var protobuf = require("..");
44

55
tape.test("proto comments in alternate-parse mode", function(test) {
6-
test.plan(24);
6+
test.plan(27);
77
var options = {alternateCommentMode: true};
88
var root = new protobuf.Root();
99
root.load("tests/data/comments-alternate-parse.proto", options, function(err, root) {
@@ -13,6 +13,9 @@ tape.test("proto comments in alternate-parse mode", function(test) {
1313
test.equal(root.lookup("Test1").comment, "Message with\na\nmulti-line comment.", "should parse double-slash multiline comment");
1414
test.equal(root.lookup("Test2").comment, "Message\nwith\na multiline plain slash-star\ncomment.", "should parse slash-star multiline comment");
1515
test.equal(root.lookup("Test3").comment, "Message\nwith\na\ncomment and stars.", "should parse doc-block multiline comment");
16+
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");
17+
test.equal(root.lookup("Test5").comment, "line comment with\nwhitespace in front", "should ignore leading whitespace when parsing line comments");
18+
test.equal(root.lookup("Test6").comment, "with strange whitespace", "should parse block comment preceeded by double slash comments with leading whitespace");
1619

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

0 commit comments

Comments
 (0)