Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1352)

Unified Diff: mojo/public/js/codec.js

Issue 803173003: Add more type validation to codec.js (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/public/js/codec_unittests.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/js/codec.js
diff --git a/mojo/public/js/codec.js b/mojo/public/js/codec.js
index 5440abed92d3651e684a492f97673bee286d9d68..f6b77ef05b27bc6cf0d773d52e5f5ae8e1f875b8 100644
--- a/mojo/public/js/codec.js
+++ b/mojo/public/js/codec.js
@@ -8,7 +8,9 @@ define("mojo/public/js/codec", [
], function(unicode, buffer) {
var kErrorUnsigned = "Passing negative value to unsigned";
- var kErrorArray = "Passing non-array-like to array";
+ var kErrorArray = "Passing non Array for array type";
+ var kErrorString = "Passing non String for string type";
+ var kErrorMap = "Passing non Map for map type";
// Memory -------------------------------------------------------------------
@@ -353,7 +355,7 @@ define("mojo/public/js/codec", [
this.encodePointer(val);
return;
}
- if (val.length === undefined) {
+ if (!(val instanceof Array)) {
throw new Error(kErrorArray);
}
var numberOfElements = val.length;
@@ -369,6 +371,10 @@ define("mojo/public/js/codec", [
this.encodePointer(val);
return;
}
+ // This only accepts literal strings, not new String("foo").
+ if (typeof(val) !== "string") {
hansmuller 2014/12/16 01:10:41 Don't you mean primitive strings?
eseidel 2014/12/16 01:13:04 I'm not sure what you mean. I mean string literal
+ throw new Error(kErrorString);
+ }
var encodedSize = kArrayHeaderSize + unicode.utf8Length(val);
var encoder = this.createAndEncodeEncoder(encodedSize);
encoder.encodeString(val);
@@ -394,6 +400,9 @@ define("mojo/public/js/codec", [
this.encodePointer(val);
return;
}
+ if (!(val instanceof Map)) {
+ throw new Error(kErrorMap);
+ }
var encodedSize = kStructHeaderSize + kMapStructPayloadSize;
var encoder = this.createAndEncodeEncoder(encodedSize);
encoder.encodeMap(keyClass, valueClass, val);
« no previous file with comments | « no previous file | mojo/public/js/codec_unittests.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698