Count the number of jumps to break out of infinite loops.

This is especially useful for the bru cli, to make sure that test-runners
that are accidentally stuck in an infinite loop still terminate in a
reasonable amount of time and don't hog up resources.
This commit is contained in:
Martin Hoecker 2023-11-01 23:15:54 +01:00
parent db0de68987
commit 129d659628
No known key found for this signature in database
GPG Key ID: DD1434854AB52DD0
2 changed files with 13 additions and 2 deletions

View File

@ -356,6 +356,7 @@ const handler = async function (argv) {
} }
let currentRequestIndex = 0; let currentRequestIndex = 0;
let nJumps = 0; // count the number of jumps to avoid infinite loops
while (currentRequestIndex < bruJsons.length) { while (currentRequestIndex < bruJsons.length) {
const iter = bruJsons[currentRequestIndex]; const iter = bruJsons[currentRequestIndex];
const { bruFilepath, bruJson } = iter; const { bruFilepath, bruJson } = iter;
@ -372,6 +373,11 @@ const handler = async function (argv) {
const nextRequestName = result?.nextRequestName; const nextRequestName = result?.nextRequestName;
if (nextRequestName) { if (nextRequestName) {
nJumps++;
if (nJumps > 10000) {
console.error(chalk.red(`Too many jumps, possible infinite loop`));
process.exit(1);
}
const nextRequestIdx = bruJsons.findIndex((iter) => iter.bruJson.name === nextRequestName); const nextRequestIdx = bruJsons.findIndex((iter) => iter.bruJson.name === nextRequestName);
if (nextRequestIdx >= 0) { if (nextRequestIdx >= 0) {
currentRequestIndex = nextRequestIdx; currentRequestIndex = nextRequestIdx;

View File

@ -673,9 +673,10 @@ const registerNetworkIpc = (mainWindow) => {
} }
let currentRequestIndex = 0; let currentRequestIndex = 0;
let nJumps = 0; // count the number of jumps to avoid infinite loops
while (currentRequestIndex < folderRequests.length) { while (currentRequestIndex < folderRequests.length) {
item = folderRequests[currentRequestIndex]; const item = folderRequests[currentRequestIndex];
let nextRequestName = undefined; let nextRequestName;
const itemUid = item.uid; const itemUid = item.uid;
const eventData = { const eventData = {
collectionUid, collectionUid,
@ -866,6 +867,10 @@ const registerNetworkIpc = (mainWindow) => {
}); });
} }
if (nextRequestName) { if (nextRequestName) {
nJumps++;
if (nJumps > 10000) {
throw new Error('Too many jumps, possible infinite loop');
}
const nextRequestIdx = folderRequests.findIndex((request) => request.name === nextRequestName); const nextRequestIdx = folderRequests.findIndex((request) => request.name === nextRequestName);
if (nextRequestIdx >= 0) { if (nextRequestIdx >= 0) {
currentRequestIndex = nextRequestIdx; currentRequestIndex = nextRequestIdx;