Skip to content

Commit

Permalink
Restrict the maximum number of layout passes in MDCAlertController t…
Browse files Browse the repository at this point in the history
…o 10.

PiperOrigin-RevId: 644022588
  • Loading branch information
loading-google authored and material-automation committed Jun 17, 2024
1 parent 54fbd86 commit 04a7740
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions components/Dialogs/src/MDCAlertController.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

NS_ASSUME_NONNULL_BEGIN

const int MAX_LAYOUT_PASSES = 10;

// The Bundle for string resources.
static NSString *const kMaterialDialogsBundle = @"MaterialDialogs.bundle";

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

/**
This counter caps the maximum number of layout passes that can be done in a single layout cycle.
This variable is added as a direct fix for b/345505157.
*/
@property(nonatomic) int layoutPassCounter;

- (nonnull instancetype)initWithTitle:(nullable NSString *)title
message:(nullable NSString *)message;

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

_layoutPassCounter = 0;
_alertTitle = [title copy];
_titleAlignment = NSTextAlignmentNatural;
_messageAlignment = NSTextAlignmentNatural;
Expand Down Expand Up @@ -726,6 +736,12 @@ - (void)viewDidDisappear:(BOOL)animated {
}

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

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

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

0 comments on commit 04a7740

Please sign in to comment.