|
|
Created:
7 years, 11 months ago by kmadhusu Modified:
7 years, 11 months ago CC:
chromium-reviews, Aaron Boodman, chromium-apps-reviews_chromium.org Base URL:
svn://svn.chromium.org/chrome/trunk/src Visibility:
Public. |
DescriptionModified extensions::StorageInfoProvider::CheckWatchedStorages() to avoid dereferencing an invalid iterator.
BUG=none
TEST=none
CID=106968
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=175191
Patch Set 1 #
Total comments: 4
Patch Set 2 : '' #
Total comments: 3
Messages
Total messages: 13 (0 generated)
https://codereview.chromium.org/11748025/diff/1/chrome/browser/extensions/api... File chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc (left): https://codereview.chromium.org/11748025/diff/1/chrome/browser/extensions/api... chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc:110: storage_id_to_size_map_.erase(it); Please avoid inserting/deleting the map elements while the map is accessed in a for loop. Because these operations invalidates the iterator and leads to an undefined behavior. https://codereview.chromium.org/11748025/diff/1/chrome/browser/extensions/api... chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc:118: if (it->second != info.available_capacity) { At (1): Condition "it != this->storage_id_to_size_map_.end()", taking true branch At (2): Condition "!this->QueryUnitInfo(it->first, &info)", taking true branch At (3): "erase" invalidates iterator "it". At (4): Condition "this->storage_id_to_size_map_.size() == 0", taking false branch At (5): Using invalid iterator "it".
https://codereview.chromium.org/11748025/diff/1/chrome/browser/extensions/api... File chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc (left): https://codereview.chromium.org/11748025/diff/1/chrome/browser/extensions/api... chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc:110: storage_id_to_size_map_.erase(it); On 2013/01/04 01:03:16, kmadhusu wrote: > Please avoid inserting/deleting the map elements while the map is accessed in a > for loop. Because these operations invalidates the iterator and leads to an > undefined behavior. Good catch! Using a list to cache the elements to be erased generally works. Alternatively, it might be better to use the following pattern which is a common practice in chromium code base: for (it = storage_id_to_size_map_.begin(); it != storage_id_to_size_map_.end(); ) { if (!QueryUnitInfo(it->first, &info)) { storage_id_to_size_map_.erase(it++); // ... continue; } else { // ... ++it; } } So that we could avoid introducing an additional caching list. PS...According to the C++ standard 23.1.2.8, it says: The insert members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements. So erasing an element when iteration should be fine but may vary from compiler. It shall be better to the above pattern to handle this case. See http://stackoverflow.com/questions/2874441/deleting-elements-from-stl-set-whi....
https://codereview.chromium.org/11748025/diff/1/chrome/browser/extensions/api... File chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc (left): https://codereview.chromium.org/11748025/diff/1/chrome/browser/extensions/api... chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc:110: storage_id_to_size_map_.erase(it); On 2013/01/04 01:50:06, Hongbo Min wrote: > On 2013/01/04 01:03:16, kmadhusu wrote: > > Please avoid inserting/deleting the map elements while the map is accessed in > a > > for loop. Because these operations invalidates the iterator and leads to an > > undefined behavior. > > Good catch! Using a list to cache the elements to be erased generally works. > Alternatively, it might be better to use the following pattern which is a common > practice in chromium code base: > > for (it = storage_id_to_size_map_.begin(); it != storage_id_to_size_map_.end(); > ) { > if (!QueryUnitInfo(it->first, &info)) { > storage_id_to_size_map_.erase(it++); > // ... > continue; > } else { > // ... > ++it; > } > } > > So that we could avoid introducing an additional caching list. > > PS...According to the C++ standard 23.1.2.8, it says: > The insert members shall not affect the validity of iterators and references to > the container, and the erase members shall invalidate only iterators and > references to the erased elements. > > So erasing an element when iteration should be fine but may vary from compiler. > It shall be better to the above pattern to handle this case. See > http://stackoverflow.com/questions/2874441/deleting-elements-from-stl-set-whi.... Sure. Done.
https://codereview.chromium.org/11748025/diff/4001/chrome/browser/extensions/... File chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc (right): https://codereview.chromium.org/11748025/diff/4001/chrome/browser/extensions/... chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc:122: if (storage_id_to_size_map_.size() == 0) { Pls move this code block right after line 109, otherwise, it should have no chance to get running.
https://codereview.chromium.org/11748025/diff/4001/chrome/browser/extensions/... File chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc (right): https://codereview.chromium.org/11748025/diff/4001/chrome/browser/extensions/... chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc:122: if (storage_id_to_size_map_.size() == 0) { On 2013/01/04 02:37:49, Hongbo Min wrote: > Pls move this code block right after line 109, otherwise, it should have no > chance to get running. This code will run as expected. Why do you think it should be within the for loop? To me, the new code is easier to read and understand.
lgtm! Thanks. https://codereview.chromium.org/11748025/diff/4001/chrome/browser/extensions/... File chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc (right): https://codereview.chromium.org/11748025/diff/4001/chrome/browser/extensions/... chrome/browser/extensions/api/system_info_storage/storage_info_provider.cc:122: if (storage_id_to_size_map_.size() == 0) { On 2013/01/04 02:45:18, kmadhusu wrote: > On 2013/01/04 02:37:49, Hongbo Min wrote: > > Pls move this code block right after line 109, otherwise, it should have no > > chance to get running. > > This code will run as expected. Why do you think it should be within the for > loop? To me, the new code is easier to read and understand. You are right. I was thinking it is in for loop scope.
No LGTM from a valid reviewer yet. Only full committers are accepted. Even if an LGTM may have been provided, it was from a non-committer or a lowly provisional committer, _not_ a full super star committer. See http://www.chromium.org/getting-involved/become-a-committer Note that this has nothing to do with OWNERS files.
benwells@: Can you please review the code? Thanks.
lgtm
CQ is trying da patch. Follow status at https://chromium-status.appspot.com/cq/kmadhusu@chromium.org/11748025/4001
Retried try job too often on win_rel for step(s) unit_tests
CQ is trying da patch. Follow status at https://chromium-status.appspot.com/cq/kmadhusu@chromium.org/11748025/4001
Message was sent while issue was closed.
Change committed as 175191 |