Skip to content

Commit

Permalink
fix: add braces around message decoder case statements so 'let' varia…
Browse files Browse the repository at this point in the history
…ble declarations cannot overlap (protobufjs#1452)
  • Loading branch information
AndiDog committed Oct 10, 2020
1 parent 80451da commit 013771b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function decoder(mtype) {
var field = mtype._fieldsArray[i].resolve(),
type = field.resolvedType instanceof Enum ? "int32" : field.type,
ref = "m" + util.safeProp(field.name); gen
("case %i:", field.id);
("case %i: {", field.id);

// Map fields
if (field.map) { gen
Expand Down Expand Up @@ -104,7 +104,7 @@ function decoder(mtype) {
else gen
("%s=r.%s()", ref, type);
gen
("break");
("break }");
// Unknown fields
} gen
("default:")
Expand Down
9 changes: 9 additions & 0 deletions tests/data/two_maps.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax="proto3";

package Example;

message Msg
{
map<string, string> one = 1;
map<string, string> two = 2;
}
87 changes: 87 additions & 0 deletions tests/node/cli_pbts_unambiguous_variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
var fs = require("fs");
var path = require("path");
var tape = require("tape");
var tmp = require("tmp");

var cli = require(path.join(__dirname, "..", "..", "cli", "pbts.js"));
var protobuf = require(path.join("..", ".."));
var static_module_target = require(path.join(__dirname, "..", "..", "cli", "targets", "static-module"));

// This proto input used to produce the same variable name `end2` in one `switch` scope of the
// JavaScript output, leading to failing pbts execution. Test that code generation works
// in this case by running pbts (variable scope reduced to each case statement).
tape.test("unambiguous variables", function(test) {
if (!protobuf.util.isNode) {
return;
}

var tmpFilePath = tmp.tmpNameSync({postfix: "tmpFilePath.js"});
var tmpFilePathTypeScriptOutput = tmp.tmpNameSync({postfix: "tmpFilePathTypeScriptOutput"});

tape.onFinish(function() {
try {
fs.unlinkSync(tmpFilePath);
fs.unlinkSync(tmpFilePathTypeScriptOutput);
} catch(e) {
// ignored
}
});

protobuf.load("tests/data/two_maps.proto", function(err, root) {
if (err || !root) {
test.fail(err && err.message || "should parse without errors");
return;
}
new protobuf.Root().load("tests/data/two_maps.proto", { keepCase: true }, function(err, root) {
if (err || !root) {
test.fail(err && err.message || "should parse without errors");
return;
}
test.pass("should parse without errors");
test.doesNotThrow(function() {
root.resolveAll();
}, "should resolve without errors");

var options = {
create: true,
encode: true,
decode: true,
verify: true,
convert: true,
delimited: true,
beautify: true,
comments: true,
es6: true,
};
static_module_target(root, options, function targetCallback(err, output) {
if (err || !output || !output.length) {
test.fail(err && err.message || "should output without errors");
return;
}

fs.writeFileSync(tmpFilePath, Buffer.from(output, "utf-8"));

function onCliCompletion(err, output) {
if (err || !output || !output.length) {
test.fail(err && err.message || "should output TypeScript without errors");
return;
}

var typeScriptOutput = fs.readFileSync(tmpFilePathTypeScriptOutput).toString();
if (!typeScriptOutput || typeScriptOutput.indexOf("Example.Msg") === -1) {
test.fail(err && err.message || "should output TypeScript without errors");
return;
}

test.end();
}

try {
cli.main(["-o", tmpFilePathTypeScriptOutput, tmpFilePath], onCliCompletion);
} catch (e) {
test.fail("pbts cli failed: " + e);
}
});
});
});
});

0 comments on commit 013771b

Please sign in to comment.