From c11037b2998f161c6ec0399a9d260f1856c4e15e Mon Sep 17 00:00:00 2001
From: Sebastian Poxhofer <secustor@users.noreply.github.com>
Date: Fri, 1 Dec 2023 07:08:03 +0100
Subject: [PATCH] fix(manager/terraform): prevent endless loop in case of
 multiple constraint elements (#26049)

---
 .../manager/terraform/lockfile/index.spec.ts        | 13 +++++++++++++
 lib/modules/manager/terraform/lockfile/index.ts     | 10 +++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/lib/modules/manager/terraform/lockfile/index.spec.ts b/lib/modules/manager/terraform/lockfile/index.spec.ts
index 93cb54e620..fb1eb92b9f 100644
--- a/lib/modules/manager/terraform/lockfile/index.spec.ts
+++ b/lib/modules/manager/terraform/lockfile/index.spec.ts
@@ -1104,5 +1104,18 @@ describe('modules/manager/terraform/lockfile/index', () => {
         ),
       ).toBe('5.26.0');
     });
+
+    it('update constraint with multiple elements', () => {
+      expect(
+        getNewConstraint(
+          {
+            currentValue: '2.41.0',
+            newValue: '2.46.0',
+            newVersion: '2.46.0',
+          },
+          '>= 2.36.0, 2.41.0',
+        ),
+      ).toBe('>= 2.36.0, 2.46.0');
+    });
   });
 });
diff --git a/lib/modules/manager/terraform/lockfile/index.ts b/lib/modules/manager/terraform/lockfile/index.ts
index f73ca9d9a3..53c6ae7e33 100644
--- a/lib/modules/manager/terraform/lockfile/index.ts
+++ b/lib/modules/manager/terraform/lockfile/index.ts
@@ -1,6 +1,7 @@
 import is from '@sindresorhus/is';
 import { logger } from '../../../../logger';
 import * as p from '../../../../util/promises';
+import { escapeRegExp, regEx } from '../../../../util/regex';
 import { GetPkgReleasesConfig, getPkgReleases } from '../../../datasource';
 import { TerraformProviderDatasource } from '../../../datasource/terraform-provider';
 import { get as getVersioning } from '../../../versioning';
@@ -88,12 +89,11 @@ export function getNewConstraint(
     logger.debug(
       `Updating constraint "${oldConstraint}" to replace "${currentValue}" with "${newValue}" for "${packageName}"`,
     );
-    let newConstraint = oldConstraint.replace(currentValue, newValue);
     //remove surplus .0 version
-    while (newConstraint.split('.').length - 1 > 2) {
-      newConstraint = newConstraint.replace(RegExp('\\.0$'), '');
-    }
-    return newConstraint;
+    return oldConstraint.replace(
+      regEx(`${escapeRegExp(currentValue)}(\\.0)*`),
+      newValue,
+    );
   }
 
   if (
-- 
GitLab