fix: prevent infinite loop in session renewal by skipping block on break/continue/return

This commit is contained in:
2025-11-23 12:53:36 +00:00
parent 67c15f026d
commit 03bca46d88
17 changed files with 708 additions and 36 deletions
+81
View File
@@ -0,0 +1,81 @@
int main() {
printf("=== Break and Continue Tests ===\n");
printf("Test 1: Break statement\n");
int count = 0;
while (count < 10) {
count = count + 1;
if (count == 5) {
printf("Breaking at count = %d\n", count);
break;
}
printf("count = %d\n", count);
}
printf("Final count after break: %d\n", count);
printf("PASS: Break works\n");
printf("Test 2: Continue statement\n");
int i = 0;
int sum = 0;
while (i < 10) {
i = i + 1;
if (i == 3 || i == 7) {
printf("Skipping i = %d\n", i);
continue;
}
sum = sum + i;
printf("Adding i = %d, sum = %d\n", i, sum);
}
printf("Final sum (skipped 3 and 7): %d\n", sum);
printf("PASS: Continue works\n");
printf("Test 3: Break in nested if\n");
int j = 0;
while (j < 10) {
j = j + 1;
if (j > 3) {
if (j == 6) {
printf("Breaking at j = %d (nested if)\n", j);
break;
}
}
printf("j = %d\n", j);
}
printf("Final j: %d\n", j);
printf("PASS: Nested break works\n");
printf("Test 4: Continue in nested if\n");
int k = 0;
int even_sum = 0;
while (k < 10) {
k = k + 1;
if (k > 0) {
int rem = k - (k / 2) * 2;
if (rem == 1) {
continue;
}
}
even_sum = even_sum + k;
}
printf("Sum of even numbers 1-10: %d\n", even_sum);
printf("PASS: Nested continue works\n");
printf("Test 5: Multiple breaks and continues\n");
int n = 0;
int result = 0;
while (n < 20) {
n = n + 1;
if (n < 5) {
continue;
}
if (n > 15) {
break;
}
result = result + 1;
}
printf("Numbers counted between 5 and 15: %d\n", result);
printf("PASS: Multiple break/continue works\n");
printf("\n=== All Break/Continue Tests Completed ===\n");
return 0;
}
+14
View File
@@ -0,0 +1,14 @@
int main() {
printf("=== Break Test ===\n");
int count = 0;
while (count < 10) {
count = count + 1;
if (count == 5) {
printf("Breaking at count = %d\n", count);
break;
}
printf("count = %d\n", count);
}
printf("Final count: %d\n", count);
return 0;
}
+13
View File
@@ -0,0 +1,13 @@
int main() {
printf("=== Break Test ===\n");
int count = 0;
while (count < 10) {
count = count + 1;
if (count == 5) {
break;
}
printf("count = %d\n", count);
}
printf("Final count: %d\n", count);
return 0;
}
+14
View File
@@ -0,0 +1,14 @@
int main() {
printf("Test OR with continue\n");
int i = 0;
while (i < 10) {
i = i + 1;
if (i == 3 || i == 7) {
printf("Skip %d\n", i);
continue;
}
printf("i = %d\n", i);
}
printf("Done\n");
return 0;
}
+14
View File
@@ -0,0 +1,14 @@
int main() {
printf("Start\n");
int i = 0;
while (i < 5) {
i = i + 1;
if (i == 3) {
printf("Skipping i = %d\n", i);
continue;
}
printf("i = %d\n", i);
}
printf("Done\n");
return 0;
}
+53
View File
@@ -0,0 +1,53 @@
int main() {
printf("=== Double Data Type Tests ===\n");
printf("Test 1: Double variable declaration and assignment\n");
double pi = 3.14159;
printf("pi = %f\n", pi);
printf("PASS: Double variable works\n");
printf("Test 2: Double arithmetic using helper functions\n");
double a = 10.5;
double b = 2.5;
printf("a = %f, b = %f\n", a, b);
double sum = double_add(a, b);
printf("a + b = %f\n", sum);
double diff = double_sub(a, b);
printf("a - b = %f\n", diff);
double prod = double_mul(a, b);
printf("a * b = %f\n", prod);
double quot = double_div(a, b);
printf("a / b = %f\n", quot);
printf("PASS: Double arithmetic works\n");
printf("Test 3: Type conversions\n");
int x = 42;
double dx = int_to_double(x);
printf("int %d converted to double: %f\n", x, dx);
double y = 99.9;
int iy = double_to_int(y);
printf("double %f converted to int: %d\n", y, iy);
printf("PASS: Type conversions work\n");
printf("Test 4: Double with mathematical functions\n");
double num = 16.0;
double sq = sqrt(double_to_int(num));
double sq_double = int_to_double(sq);
printf("sqrt(%f) = %f\n", num, sq_double);
printf("PASS: Math functions work with doubles\n");
printf("Test 5: Complex calculation\n");
double radius = 5.0;
double pi2 = 3.14159;
double area = double_mul(pi2, double_mul(radius, radius));
printf("Circle area (r=%f): %f\n", radius, area);
printf("PASS: Complex calculations work\n");
printf("\n=== All Double Tests Completed ===\n");
return 0;
}
+82
View File
@@ -0,0 +1,82 @@
int main() {
printf("=== File I/O Tests ===\n");
printf("Test 1: Writing to a file\n");
int f = fopen("test_output.txt", "w");
if (f == 0) {
printf("ERROR: Could not open file for writing\n");
return 1;
}
fputs(f, "Hello, File I/O!\n");
fputs(f, "This is line 2.\n");
fputs(f, "Testing file operations.\n");
fclose(f);
printf("PASS: File written successfully\n");
printf("Test 2: Reading from file using fgets\n");
f = fopen("test_output.txt", "r");
if (f == 0) {
printf("ERROR: Could not open file for reading\n");
return 1;
}
char *line1 = fgets(f, 256);
printf("Line 1: %s", line1);
char *line2 = fgets(f, 256);
printf("Line 2: %s", line2);
char *line3 = fgets(f, 256);
printf("Line 3: %s", line3);
fclose(f);
printf("PASS: File read successfully\n");
printf("Test 3: File position operations\n");
f = fopen("test_output.txt", "r");
if (f == 0) {
printf("ERROR: Could not open file\n");
return 1;
}
int pos = ftell(f);
printf("Initial position: %d\n", pos);
fseek(f, 7, SEEK_SET());
pos = ftell(f);
printf("Position after seek: %d\n", pos);
char *partial = fgets(f, 256);
printf("Read after seek: %s", partial);
fclose(f);
printf("PASS: File positioning works\n");
printf("Test 4: End of file detection\n");
f = fopen("test_output.txt", "r");
if (f == 0) {
printf("ERROR: Could not open file\n");
return 1;
}
int line_count = 0;
while (feof(f) == 0) {
char *line = fgets(f, 256);
if (strlen(line) > 0) {
line_count = line_count + 1;
}
}
printf("Total lines read: %d\n", line_count);
fclose(f);
printf("PASS: EOF detection works\n");
printf("Test 5: File rename operation\n");
int result = frename("test_output.txt", "test_renamed.txt");
if (result == 0) {
printf("PASS: File renamed successfully\n");
} else {
printf("ERROR: File rename failed\n");
}
printf("Test 6: File removal\n");
result = fremove("test_renamed.txt");
if (result == 0) {
printf("PASS: File removed successfully\n");
} else {
printf("ERROR: File removal failed\n");
}
printf("\n=== All File I/O Tests Completed ===\n");
return 0;
}
+9
View File
@@ -0,0 +1,9 @@
int main() {
printf("Test OR\n");
int i = 3;
if (i == 3 || i == 7) {
printf("Match\n");
}
printf("Done\n");
return 0;
}
+13
View File
@@ -0,0 +1,13 @@
int main() {
printf("Start\n");
int i = 0;
while (i < 5) {
i = i + 1;
printf("i = %d\n", i);
if (i == 3) {
break;
}
}
printf("After loop, i = %d\n", i);
return 0;
}