Commit b4cc1348 authored by edchin's avatar edchin Committed by Commit Bot

[ios] Use checked_cast in list_model

Numeric/integral conversions can suffer from overflow so checked_cast
should be used instead of static_cast.

Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ib5c646f1ae599cf95f594a4d317745407f4a6f17
Reviewed-on: https://chromium-review.googlesource.com/1058333Reviewed-by: default avataredchin <edchin@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Commit-Queue: edchin <edchin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558523}
parent c6ff060c
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#import "ios/chrome/browser/ui/list_model/list_model.h" #import "ios/chrome/browser/ui/list_model/list_model.h"
#include "base/logging.h" #include "base/logging.h"
#import "base/numerics/safe_conversions.h"
#import "ios/chrome/browser/ui/list_model/list_item.h" #import "ios/chrome/browser/ui/list_model/list_item.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -41,7 +42,7 @@ typedef NSMutableArray<ListItem*> SectionItems; ...@@ -41,7 +42,7 @@ typedef NSMutableArray<ListItem*> SectionItems;
- (void)addSectionWithIdentifier:(NSInteger)sectionIdentifier { - (void)addSectionWithIdentifier:(NSInteger)sectionIdentifier {
DCHECK_GE(sectionIdentifier, kSectionIdentifierEnumZero); DCHECK_GE(sectionIdentifier, kSectionIdentifierEnumZero);
DCHECK_EQ(static_cast<NSUInteger>(NSNotFound), DCHECK_EQ(base::checked_cast<NSUInteger>(NSNotFound),
[self internalSectionForIdentifier:sectionIdentifier]); [self internalSectionForIdentifier:sectionIdentifier]);
[_sectionIdentifiers addObject:@(sectionIdentifier)]; [_sectionIdentifiers addObject:@(sectionIdentifier)];
...@@ -52,7 +53,7 @@ typedef NSMutableArray<ListItem*> SectionItems; ...@@ -52,7 +53,7 @@ typedef NSMutableArray<ListItem*> SectionItems;
- (void)insertSectionWithIdentifier:(NSInteger)sectionIdentifier - (void)insertSectionWithIdentifier:(NSInteger)sectionIdentifier
atIndex:(NSUInteger)index { atIndex:(NSUInteger)index {
DCHECK_GE(sectionIdentifier, kSectionIdentifierEnumZero); DCHECK_GE(sectionIdentifier, kSectionIdentifierEnumZero);
DCHECK_EQ(static_cast<NSUInteger>(NSNotFound), DCHECK_EQ(base::checked_cast<NSUInteger>(NSNotFound),
[self internalSectionForIdentifier:sectionIdentifier]); [self internalSectionForIdentifier:sectionIdentifier]);
DCHECK_LE(index, [_sections count]); DCHECK_LE(index, [_sections count]);
...@@ -127,7 +128,8 @@ typedef NSMutableArray<ListItem*> SectionItems; ...@@ -127,7 +128,8 @@ typedef NSMutableArray<ListItem*> SectionItems;
#pragma mark Query model coordinates from index paths #pragma mark Query model coordinates from index paths
- (NSInteger)sectionIdentifierForSection:(NSInteger)section { - (NSInteger)sectionIdentifierForSection:(NSInteger)section {
DCHECK_LT(static_cast<NSUInteger>(section), [_sectionIdentifiers count]); DCHECK_LT(base::checked_cast<NSUInteger>(section),
[_sectionIdentifiers count]);
return [[_sectionIdentifiers objectAtIndex:section] integerValue]; return [[_sectionIdentifiers objectAtIndex:section] integerValue];
} }
...@@ -136,7 +138,8 @@ typedef NSMutableArray<ListItem*> SectionItems; ...@@ -136,7 +138,8 @@ typedef NSMutableArray<ListItem*> SectionItems;
} }
- (NSUInteger)indexInItemTypeForIndexPath:(NSIndexPath*)indexPath { - (NSUInteger)indexInItemTypeForIndexPath:(NSIndexPath*)indexPath {
DCHECK_LT(static_cast<NSUInteger>(indexPath.section), [_sections count]); DCHECK_LT(base::checked_cast<NSUInteger>(indexPath.section),
[_sections count]);
SectionItems* items = [_sections objectAtIndex:indexPath.section]; SectionItems* items = [_sections objectAtIndex:indexPath.section];
ListItem* item = [self itemAtIndexPath:indexPath]; ListItem* item = [self itemAtIndexPath:indexPath];
...@@ -151,19 +154,20 @@ typedef NSMutableArray<ListItem*> SectionItems; ...@@ -151,19 +154,20 @@ typedef NSMutableArray<ListItem*> SectionItems;
if (!indexPath) if (!indexPath)
return NO; return NO;
if (static_cast<NSUInteger>(indexPath.section) < [_sections count]) { if (base::checked_cast<NSUInteger>(indexPath.section) < [_sections count]) {
SectionItems* items = [_sections objectAtIndex:indexPath.section]; SectionItems* items = [_sections objectAtIndex:indexPath.section];
return static_cast<NSUInteger>(indexPath.item) < [items count]; return base::checked_cast<NSUInteger>(indexPath.item) < [items count];
} }
return NO; return NO;
} }
- (ListItem*)itemAtIndexPath:(NSIndexPath*)indexPath { - (ListItem*)itemAtIndexPath:(NSIndexPath*)indexPath {
DCHECK(indexPath); DCHECK(indexPath);
DCHECK_LT(static_cast<NSUInteger>(indexPath.section), [_sections count]); DCHECK_LT(base::checked_cast<NSUInteger>(indexPath.section),
[_sections count]);
SectionItems* items = [_sections objectAtIndex:indexPath.section]; SectionItems* items = [_sections objectAtIndex:indexPath.section];
DCHECK_LT(static_cast<NSUInteger>(indexPath.item), [items count]); DCHECK_LT(base::checked_cast<NSUInteger>(indexPath.item), [items count]);
return [items objectAtIndex:indexPath.item]; return [items objectAtIndex:indexPath.item];
} }
...@@ -182,7 +186,7 @@ typedef NSMutableArray<ListItem*> SectionItems; ...@@ -182,7 +186,7 @@ typedef NSMutableArray<ListItem*> SectionItems;
- (NSArray<ListItem*>*)itemsInSectionWithIdentifier: - (NSArray<ListItem*>*)itemsInSectionWithIdentifier:
(NSInteger)sectionIdentifier { (NSInteger)sectionIdentifier {
NSInteger section = [self sectionForSectionIdentifier:sectionIdentifier]; NSInteger section = [self sectionForSectionIdentifier:sectionIdentifier];
DCHECK_LT(static_cast<NSUInteger>(section), [_sections count]); DCHECK_LT(base::checked_cast<NSUInteger>(section), [_sections count]);
return [_sections objectAtIndex:section]; return [_sections objectAtIndex:section];
} }
...@@ -200,12 +204,12 @@ typedef NSMutableArray<ListItem*> SectionItems; ...@@ -200,12 +204,12 @@ typedef NSMutableArray<ListItem*> SectionItems;
- (BOOL)hasSectionForSectionIdentifier:(NSInteger)sectionIdentifier { - (BOOL)hasSectionForSectionIdentifier:(NSInteger)sectionIdentifier {
NSUInteger section = [self internalSectionForIdentifier:sectionIdentifier]; NSUInteger section = [self internalSectionForIdentifier:sectionIdentifier];
return section != static_cast<NSUInteger>(NSNotFound); return section != base::checked_cast<NSUInteger>(NSNotFound);
} }
- (NSInteger)sectionForSectionIdentifier:(NSInteger)sectionIdentifier { - (NSInteger)sectionForSectionIdentifier:(NSInteger)sectionIdentifier {
NSUInteger section = [self internalSectionForIdentifier:sectionIdentifier]; NSUInteger section = [self internalSectionForIdentifier:sectionIdentifier];
DCHECK_NE(static_cast<NSUInteger>(NSNotFound), section); DCHECK_NE(base::checked_cast<NSUInteger>(NSNotFound), section);
return section; return section;
} }
...@@ -280,7 +284,7 @@ typedef NSMutableArray<ListItem*> SectionItems; ...@@ -280,7 +284,7 @@ typedef NSMutableArray<ListItem*> SectionItems;
} }
- (NSInteger)numberOfItemsInSection:(NSInteger)section { - (NSInteger)numberOfItemsInSection:(NSInteger)section {
DCHECK_LT(static_cast<NSUInteger>(section), [_sections count]); DCHECK_LT(base::checked_cast<NSUInteger>(section), [_sections count]);
SectionItems* items = [_sections objectAtIndex:section]; SectionItems* items = [_sections objectAtIndex:section];
return items.count; return items.count;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment