Skip to content

Commit

Permalink
Breaking: fromObject now throws for entirely bogus values (repeated, …
Browse files Browse the repository at this point in the history
…map and inner message fields), fixes #601
  • Loading branch information
dcodeIO committed Jan 20, 2017
1 parent b4f4f48 commit dca26ba
Show file tree
Hide file tree
Showing 18 changed files with 429 additions and 82 deletions.
10 changes: 8 additions & 2 deletions dist/light/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/light/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/light/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/light/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/light/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/minimal/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/minimal/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/minimal/protobuf.min.js.gz
Binary file not shown.
10 changes: 8 additions & 2 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
}); gen
("}");
} else gen
("if(typeof d%s!==\"object\")", prop)
("throw TypeError(%j)", field.fullName + ": object expected")
("m%s=types[%d].fromObject(d%s)", prop, fieldIndex, prop);
} else {
var isUnsigned = false;
Expand Down Expand Up @@ -70,7 +72,7 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
case "bytes": gen
("if(typeof d%s===\"string\")", prop)
("util.base64.decode(d%s,m%s=util.newBuffer(util.base64.length(d%s)),0)", prop, prop, prop)
("else if(d%s&&d%s.length)", prop, prop)
("else if(d%s.length)", prop)
("m%s=d%s", prop, prop);
break;
case "string": gen
Expand Down Expand Up @@ -110,6 +112,8 @@ converter.fromObject = function fromObject(mtype) {
// Map fields
if (field.map) { gen
("if(d%s){", prop)
("if(typeof d%s!==\"object\")", prop)
("throw TypeError(%j)", field.fullName + ": object expected")
("m%s={}", prop)
("for(var ks=Object.keys(d%s),i=0;i<ks.length;++i){", prop);
genValuePartial_fromObject(gen, field, i, prop + "[ks[i]]")
Expand All @@ -119,6 +123,8 @@ converter.fromObject = function fromObject(mtype) {
// Repeated fields
} else if (field.repeated) { gen
("if(d%s){", prop)
("if(!Array.isArray(d%s))", prop)
("throw TypeError(%j)", field.fullName + ": array expected")
("m%s=[]", prop)
("for(var i=0;i<d%s.length;++i){", prop);
genValuePartial_fromObject(gen, field, i, prop + "[i]")
Expand Down
14 changes: 12 additions & 2 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ $root.Message = (function() {
if (object.stringVal !== undefined && object.stringVal !== null)
message.stringVal = String(object.stringVal);
if (object.stringRepeated) {
if (!Array.isArray(object.stringRepeated))
throw TypeError(".Message.stringRepeated: array expected");
message.stringRepeated = [];
for (var i = 0; i < object.stringRepeated.length; ++i)
message.stringRepeated[i] = String(object.stringRepeated[i]);
Expand All @@ -325,6 +327,8 @@ $root.Message = (function() {
else if (typeof object.uint64Val === "object")
message.uint64Val = new $util.LongBits(object.uint64Val.low, object.uint64Val.high).toNumber(true);
if (object.uint64Repeated) {
if (!Array.isArray(object.uint64Repeated))
throw TypeError(".Message.uint64Repeated: array expected");
message.uint64Repeated = [];
for (var i = 0; i < object.uint64Repeated.length; ++i)
if ($util.Long)
Expand All @@ -339,14 +343,16 @@ $root.Message = (function() {
if (object.bytesVal !== undefined && object.bytesVal !== null)
if (typeof object.bytesVal === "string")
$util.base64.decode(object.bytesVal, message.bytesVal = $util.newBuffer($util.base64.length(object.bytesVal)), 0);
else if (object.bytesVal && object.bytesVal.length)
else if (object.bytesVal.length)
message.bytesVal = object.bytesVal;
if (object.bytesRepeated) {
if (!Array.isArray(object.bytesRepeated))
throw TypeError(".Message.bytesRepeated: array expected");
message.bytesRepeated = [];
for (var i = 0; i < object.bytesRepeated.length; ++i)
if (typeof object.bytesRepeated[i] === "string")
$util.base64.decode(object.bytesRepeated[i], message.bytesRepeated[i] = $util.newBuffer($util.base64.length(object.bytesRepeated[i])), 0);
else if (object.bytesRepeated[i] && object.bytesRepeated[i].length)
else if (object.bytesRepeated[i].length)
message.bytesRepeated[i] = object.bytesRepeated[i];
}
switch (object.enumVal) {
Expand All @@ -360,6 +366,8 @@ $root.Message = (function() {
break;
}
if (object.enumRepeated) {
if (!Array.isArray(object.enumRepeated))
throw TypeError(".Message.enumRepeated: array expected");
message.enumRepeated = [];
for (var i = 0; i < object.enumRepeated.length; ++i)
switch (object.enumRepeated[i]) {
Expand All @@ -375,6 +383,8 @@ $root.Message = (function() {
}
}
if (object.int64Map) {
if (typeof object.int64Map !== "object")
throw TypeError(".Message.int64Map: object expected");
message.int64Map = {};
for (var keys = Object.keys(object.int64Map), i = 0; i < keys.length; ++i)
if ($util.Long)
Expand Down
27 changes: 24 additions & 3 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,14 @@ $root.vector_tile = (function() {
return object;
var message = new $root.vector_tile.Tile();
if (object.layers) {
if (!Array.isArray(object.layers))
throw TypeError(".vector_tile.Tile.layers: array expected");
message.layers = [];
for (var i = 0; i < object.layers.length; ++i)
for (var i = 0; i < object.layers.length; ++i) {
if (typeof object.layers[i] !== "object")
throw TypeError(".vector_tile.Tile.layers: object expected");
message.layers[i] = $types[0].fromObject(object.layers[i]);
}
}
return message;
};
Expand Down Expand Up @@ -744,6 +749,8 @@ $root.vector_tile = (function() {
else if (typeof object.id === "object")
message.id = new $util.LongBits(object.id.low, object.id.high).toNumber(true);
if (object.tags) {
if (!Array.isArray(object.tags))
throw TypeError(".vector_tile.Tile.Feature.tags: array expected");
message.tags = [];
for (var i = 0; i < object.tags.length; ++i)
message.tags[i] = object.tags[i] >>> 0;
Expand All @@ -767,6 +774,8 @@ $root.vector_tile = (function() {
break;
}
if (object.geometry) {
if (!Array.isArray(object.geometry))
throw TypeError(".vector_tile.Tile.Feature.geometry: array expected");
message.geometry = [];
for (var i = 0; i < object.geometry.length; ++i)
message.geometry[i] = object.geometry[i] >>> 0;
Expand Down Expand Up @@ -1057,19 +1066,31 @@ $root.vector_tile = (function() {
if (object.name !== undefined && object.name !== null)
message.name = String(object.name);
if (object.features) {
if (!Array.isArray(object.features))
throw TypeError(".vector_tile.Tile.Layer.features: array expected");
message.features = [];
for (var i = 0; i < object.features.length; ++i)
for (var i = 0; i < object.features.length; ++i) {
if (typeof object.features[i] !== "object")
throw TypeError(".vector_tile.Tile.Layer.features: object expected");
message.features[i] = $types[2].fromObject(object.features[i]);
}
}
if (object.keys) {
if (!Array.isArray(object.keys))
throw TypeError(".vector_tile.Tile.Layer.keys: array expected");
message.keys = [];
for (var i = 0; i < object.keys.length; ++i)
message.keys[i] = String(object.keys[i]);
}
if (object.values) {
if (!Array.isArray(object.values))
throw TypeError(".vector_tile.Tile.Layer.values: array expected");
message.values = [];
for (var i = 0; i < object.values.length; ++i)
for (var i = 0; i < object.values.length; ++i) {
if (typeof object.values[i] !== "object")
throw TypeError(".vector_tile.Tile.Layer.values: object expected");
message.values[i] = $types[4].fromObject(object.values[i]);
}
}
if (object.extent !== undefined && object.extent !== null)
message.extent = object.extent >>> 0;
Expand Down
19 changes: 18 additions & 1 deletion tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,47 +451,64 @@ $root.Package = (function() {
message.author = String(object.author);
if (object.license !== undefined && object.license !== null)
message.license = String(object.license);
if (object.repository !== undefined && object.repository !== null)
if (object.repository !== undefined && object.repository !== null) {
if (typeof object.repository !== "object")
throw TypeError(".Package.repository: object expected");
message.repository = $types[6].fromObject(object.repository);
}
if (object.bugs !== undefined && object.bugs !== null)
message.bugs = String(object.bugs);
if (object.homepage !== undefined && object.homepage !== null)
message.homepage = String(object.homepage);
if (object.keywords) {
if (!Array.isArray(object.keywords))
throw TypeError(".Package.keywords: array expected");
message.keywords = [];
for (var i = 0; i < object.keywords.length; ++i)
message.keywords[i] = String(object.keywords[i]);
}
if (object.main !== undefined && object.main !== null)
message.main = String(object.main);
if (object.bin) {
if (typeof object.bin !== "object")
throw TypeError(".Package.bin: object expected");
message.bin = {};
for (var keys = Object.keys(object.bin), i = 0; i < keys.length; ++i)
message.bin[keys[i]] = String(object.bin[keys[i]]);
}
if (object.scripts) {
if (typeof object.scripts !== "object")
throw TypeError(".Package.scripts: object expected");
message.scripts = {};
for (var keys = Object.keys(object.scripts), i = 0; i < keys.length; ++i)
message.scripts[keys[i]] = String(object.scripts[keys[i]]);
}
if (object.dependencies) {
if (typeof object.dependencies !== "object")
throw TypeError(".Package.dependencies: object expected");
message.dependencies = {};
for (var keys = Object.keys(object.dependencies), i = 0; i < keys.length; ++i)
message.dependencies[keys[i]] = String(object.dependencies[keys[i]]);
}
if (object.optionalDependencies) {
if (typeof object.optionalDependencies !== "object")
throw TypeError(".Package.optionalDependencies: object expected");
message.optionalDependencies = {};
for (var keys = Object.keys(object.optionalDependencies), i = 0; i < keys.length; ++i)
message.optionalDependencies[keys[i]] = String(object.optionalDependencies[keys[i]]);
}
if (object.devDependencies) {
if (typeof object.devDependencies !== "object")
throw TypeError(".Package.devDependencies: object expected");
message.devDependencies = {};
for (var keys = Object.keys(object.devDependencies), i = 0; i < keys.length; ++i)
message.devDependencies[keys[i]] = String(object.devDependencies[keys[i]]);
}
if (object.types !== undefined && object.types !== null)
message.types = String(object.types);
if (object.cliDependencies) {
if (!Array.isArray(object.cliDependencies))
throw TypeError(".Package.cliDependencies: array expected");
message.cliDependencies = [];
for (var i = 0; i < object.cliDependencies.length; ++i)
message.cliDependencies[i] = String(object.cliDependencies[i]);
Expand Down
Loading

0 comments on commit dca26ba

Please sign in to comment.