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

Unified Diff: remoting/webapp/host_list.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
Index: remoting/webapp/host_list.js
diff --git a/remoting/webapp/host_list.js b/remoting/webapp/host_list.js
index e47bf5e842ae3ea0047d16bf366431645c54747d..bbf85374243b0a624f1184fdd04ca0a399ac8c4a 100644
--- a/remoting/webapp/host_list.js
+++ b/remoting/webapp/host_list.js
@@ -52,10 +52,11 @@ remoting.HostList = function(table, errorDiv) {
var cached = /** @type {string} */
(window.localStorage.getItem(remoting.HostList.HOSTS_KEY));
if (cached) {
- try {
- this.hosts_ = /** @type {Array} */ JSON.parse(cached);
- } catch (err) {
- console.error('Invalid host list cache:', /** @type {*} */(err));
+ var cached_parsed = jsonParseSafe(cached);
Wez 2012/04/28 00:00:42 nit: cached -> cachedStr, cached_parsed -> cached?
Jamie 2012/04/28 00:15:41 Done.
+ if (cached_parsed) {
+ this.hosts_ = /** @type {Array} */ cached_parsed;
+ } else {
+ console.error('Invalid value for ' + remoting.HostList.HOSTS_KEY);
}
}
};
@@ -122,8 +123,10 @@ remoting.HostList.prototype.parseHostListResponse_ = function(xhr, onDone) {
try {
if (xhr.status == 200) {
var parsed_response =
Wez 2012/04/28 00:00:42 nit: parsed_response -> parsedResponse, or just re
Jamie 2012/04/28 00:15:41 Done.
- /** @type {{data: {items: Array}}} */ JSON.parse(xhr.responseText);
- if (parsed_response.data && parsed_response.data.items) {
+ /** @type {{data: {items: Array}}} */ jsonParseSafe(xhr.responseText);
+ if (parsed_response &&
+ parsed_response.data &&
+ parsed_response.data.items) {
this.hosts_ = parsed_response.data.items;
/**
* @param {remoting.Host} a
@@ -138,6 +141,8 @@ remoting.HostList.prototype.parseHostListResponse_ = function(xhr, onDone) {
return 0;
};
this.hosts_ = /** @type {Array} */ this.hosts_.sort(cmp);
+ } else {
+ console.error('Invalid "hosts" response from server.');
Wez 2012/04/28 00:00:42 nit: Suggest "Invalid host list from server."
Jamie 2012/04/28 00:15:41 I'd prefer to keep the API mentioned explicitly in
}
} else {
// Some other error.

Powered by Google App Engine
This is Rietveld 408576698