Skip to content

Commit 0fe6d61

Browse files
authored
fix(cdk/clipboard): avoid infinite attempt loop (#33366)
`CdkCopyToClipboard.copy` can go into an infinite loop if a large number is passed into `attempts`. These changes add a 50 attempt maximum.
1 parent 137961a commit 0fe6d61

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

src/cdk/clipboard/copy-to-clipboard.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ export class CdkCopyToClipboard implements OnDestroy {
4949

5050
/**
5151
* How many times to attempt to copy the text. This may be necessary for longer text, because
52-
* the browser needs time to fill an intermediate textarea element and copy the content.
52+
* the browser needs time to fill an intermediate textarea element and copy the content. Attempts
53+
* are cappted out at 50.
5354
*/
5455
@Input('cdkCopyToClipboardAttempts') attempts: number = 1;
5556

@@ -78,6 +79,9 @@ export class CdkCopyToClipboard implements OnDestroy {
7879

7980
/** Copies the current text to the clipboard. */
8081
copy(attempts: number = this.attempts): void {
82+
// Avoid triggering an infinite loop if a large number of attempts is passed in.
83+
attempts = Math.min(attempts, 50);
84+
8185
if (attempts > 1) {
8286
let remainingAttempts = attempts;
8387
const pending = this._clipboard.beginCopy(this.text);

0 commit comments

Comments
 (0)