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

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: Reviewer comments. 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
« no previous file with comments | « remoting/webapp/host_controller.js ('k') | remoting/webapp/oauth2.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: remoting/webapp/host_list.js
diff --git a/remoting/webapp/host_list.js b/remoting/webapp/host_list.js
index e47bf5e842ae3ea0047d16bf366431645c54747d..c35eebaa491aa340079adad8abba37e71a83bc3a 100644
--- a/remoting/webapp/host_list.js
+++ b/remoting/webapp/host_list.js
@@ -49,13 +49,14 @@ remoting.HostList = function(table, errorDiv) {
this.lastError_ = '';
// Load the cache of the last host-list, if present.
- var cached = /** @type {string} */
+ var cachedStr = /** @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));
+ if (cachedStr) {
+ var cached = jsonParseSafe(cachedStr);
+ if (cached) {
+ this.hosts_ = /** @type {Array} */ cached;
+ } else {
+ console.error('Invalid value for ' + remoting.HostList.HOSTS_KEY);
}
}
};
@@ -121,10 +122,10 @@ remoting.HostList.prototype.parseHostListResponse_ = function(xhr, onDone) {
this.lastError_ = '';
try {
if (xhr.status == 200) {
- var parsed_response =
- /** @type {{data: {items: Array}}} */ JSON.parse(xhr.responseText);
- if (parsed_response.data && parsed_response.data.items) {
- this.hosts_ = parsed_response.data.items;
+ var response =
+ /** @type {{data: {items: Array}}} */ jsonParseSafe(xhr.responseText);
+ if (response && response.data && response.data.items) {
+ this.hosts_ = response.data.items;
/**
* @param {remoting.Host} a
* @param {remoting.Host} b
@@ -138,6 +139,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.');
}
} else {
// Some other error.
« no previous file with comments | « remoting/webapp/host_controller.js ('k') | remoting/webapp/oauth2.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698