Skip to content

Commit 04a7740

Browse files
loading-googlematerial-automation
authored andcommitted
Restrict the maximum number of layout passes in MDCAlertController to 10.
PiperOrigin-RevId: 644022588
1 parent 54fbd86 commit 04a7740

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

components/Dialogs/src/MDCAlertController.m

+18
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
NS_ASSUME_NONNULL_BEGIN
3636

37+
const int MAX_LAYOUT_PASSES = 10;
38+
3739
// The Bundle for string resources.
3840
static NSString *const kMaterialDialogsBundle = @"MaterialDialogs.bundle";
3941

@@ -105,6 +107,13 @@ @interface MDCAlertController () <UITextViewDelegate>
105107
@property(nonatomic, nonnull, strong) MDCAlertActionManager *actionManager;
106108
@property(nonatomic, nullable, strong) UIView *titleIconView;
107109

110+
/**
111+
This counter caps the maximum number of layout passes that can be done in a single layout cycle.
112+
113+
This variable is added as a direct fix for b/345505157.
114+
*/
115+
@property(nonatomic) int layoutPassCounter;
116+
108117
- (nonnull instancetype)initWithTitle:(nullable NSString *)title
109118
message:(nullable NSString *)message;
110119

@@ -173,6 +182,7 @@ - (nonnull instancetype)initWithTitle:(nullable NSString *)title {
173182
if (self) {
174183
_transitionController = [[MDCDialogTransitionController alloc] init];
175184

185+
_layoutPassCounter = 0;
176186
_alertTitle = [title copy];
177187
_titleAlignment = NSTextAlignmentNatural;
178188
_messageAlignment = NSTextAlignmentNatural;
@@ -726,6 +736,12 @@ - (void)viewDidDisappear:(BOOL)animated {
726736
}
727737

728738
- (void)viewDidLayoutSubviews {
739+
// Increments the counter to account for an additional layout pass.
740+
self.layoutPassCounter += 1;
741+
// Abort if the layout pass counter is too high.
742+
if (self.layoutPassCounter > MAX_LAYOUT_PASSES) {
743+
return;
744+
}
729745
// Recalculate preferredContentSize and potentially the view frame.
730746
BOOL boundsSizeChanged =
731747
!CGSizeEqualToSize(CGRectStandardize(self.view.bounds).size, _previousLayoutSize);
@@ -752,6 +768,8 @@ - (void)viewDidLayoutSubviews {
752768

753769
- (void)viewWillLayoutSubviews {
754770
[super viewWillLayoutSubviews];
771+
// Resets counter as this function is called at the beginning of a new layout cycle.
772+
self.layoutPassCounter = 0;
755773

756774
// Recalculate preferredSize, which is based on width available, if the viewSize has changed.
757775
if (CGRectGetWidth(self.view.bounds) != _previousLayoutSize.width ||

0 commit comments

Comments
 (0)