Permalink
Browse files

Breaking: fromObject now throws for entirely bogus values (repeated, …

…map and inner message fields), fixes #601
  • Loading branch information...
dcodeIO committed Jan 20, 2017
1 parent b4f4f48 commit dca26badfb843a597f81e98738e2fda3f66c7341

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +67 Bytes (100%) dist/light/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
BIN -1 Byte (100%) dist/minimal/protobuf.min.js.gz
Binary file not shown.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +51 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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;
@@ -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
@@ -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]]")
@@ -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]")
@@ -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]);
@@ -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)
@@ -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) {
@@ -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]) {
@@ -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)
@@ -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;
};
@@ -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;
@@ -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;
@@ -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;
@@ -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]);
Oops, something went wrong.

0 comments on commit dca26ba

Please sign in to comment.