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

Unified Diff: remoting/webapp/remoting.js

Issue 10221021: Protect all uses of JSON.parse against exceptions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Log errors if JSON parsing fails. Created 8 years, 8 months 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
« remoting/webapp/oauth2.js ('K') | « remoting/webapp/oauth2.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: remoting/webapp/remoting.js
diff --git a/remoting/webapp/remoting.js b/remoting/webapp/remoting.js
index e7836c85b7ccaee6ec1fd411231e9c04da3d94a7..a51407e951ac8343a950141aaa1def9188e089da 100644
--- a/remoting/webapp/remoting.js
+++ b/remoting/webapp/remoting.js
@@ -127,9 +127,13 @@ remoting.logExtensionInfoAsync_ = function() {
xhr.onload = function(e) {
var manifest =
/** @type {{name: string, version: string, default_locale: string}} */
- JSON.parse(xhr.responseText);
- var name = chrome.i18n.getMessage('PRODUCT_NAME');
- console.log(name + ' version: ' + manifest.version);
+ jsonParseSafe(xhr.responseText);
Wez 2012/04/28 00:00:42 I'd suggest leaving this one JSON.parse(), so that
Jamie 2012/04/28 00:15:41 TBH, I'm pretty sure it won't even load if the man
+ if (manifest) {
+ var name = chrome.i18n.getMessage('PRODUCT_NAME');
+ console.log(name + ' version: ' + manifest.version);
+ } else {
+ console.error('Failed to get product version. Corrupt manifest?');
+ }
}
xhr.send(null);
};
@@ -276,3 +280,15 @@ function getUrlParameters_() {
}
return result;
}
+
+/**
+ * @param {string} jsonString A JSON-encoded string.
+ * @return {*} The decoded object, or undefined if the string cannot be parsed.
+ */
+function jsonParseSafe(jsonString) {
Wez 2012/04/28 00:00:42 nit: parseJsonOrNull?
Jamie 2012/04/28 00:15:41 That was what I called it originally, but it doesn
Wez 2012/04/28 00:32:03 In that case safeParseJson(), then? :)
+ try {
+ return JSON.parse(jsonString);
+ } catch (err) {
+ return undefined;
Wez 2012/04/28 00:00:42 Is the aim in returning undefined that if the call
Jamie 2012/04/28 00:15:41 No. JSON.parse("null") legitimately returns null a
+ }
+}
« remoting/webapp/oauth2.js ('K') | « remoting/webapp/oauth2.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698