fixed: input parsing no longer accepts bad inputs + memory leak fixed

This commit is contained in:
Cem
2026-07-08 00:25:20 +02:00
parent a8abf55717
commit ecc4e267e7
+39 -10
View File
@@ -218,8 +218,11 @@ int main(int argc, char** argv)
switch (opt_long) switch (opt_long)
{ {
case 'V': // done, function needed case 'V': // done, function needed
version_to_use = strtol(optarg,NULL,10); char *end;
if (version_to_use < 0 || version_to_use > 5) version_to_use = strtol(optarg,&end,10);
if (version_to_use < 0 || version_to_use > 5 || end == optarg || *end != '\0')
{ {
fprintf(stderr,"Invalid version to use: V%d. Available versions are : V0,V1,V2,V3,V4,V5\n", version_to_use); fprintf(stderr,"Invalid version to use: V%d. Available versions are : V0,V1,V2,V3,V4,V5\n", version_to_use);
return EXIT_FAILURE; return EXIT_FAILURE;
@@ -232,12 +235,14 @@ int main(int argc, char** argv)
// gesetzt wird, soll ebenfalls die Hauptimplementierung ausgeführt werden // gesetzt wird, soll ebenfalls die Hauptimplementierung ausgeführt werden
break; break;
case 'B': // done, function needed case 'B': { // done, function needed
// wird die Laufzeit der angegebenen Implementierung gemessen und ausgegeben (do this later) // wird die Laufzeit der angegebenen Implementierung gemessen und ausgegeben (do this later)
amount_function_called = strtol(optarg,NULL,10); char *end;
if (amount_function_called <= 0) { amount_function_called = strtol(optarg,&end,10);
if (amount_function_called <= 0 || end == optarg || *end != '\0') {
fprintf(stderr, "Invalid benchmarking input, the value should be greater than 0\n"); fprintf(stderr, "Invalid benchmarking input, the value should be greater than 0\n");
return 1; return EXIT_FAILURE;
} }
will_benchmark = true; will_benchmark = true;
printf("amount function called is %d\n", amount_function_called); printf("amount function called is %d\n", amount_function_called);
@@ -245,6 +250,7 @@ int main(int argc, char** argv)
// Das Argument dieser Option gibt die Anzahl an Wiederholungen des Funktionsaufrufs an, z. B.-B 5 . // Das Argument dieser Option gibt die Anzahl an Wiederholungen des Funktionsaufrufs an, z. B.-B 5 .
break; break;
}
case 's': { case 's': {
sGiven = true; sGiven = true;
@@ -296,18 +302,27 @@ int main(int argc, char** argv)
break; break;
} }
case 'n': case 'n': {
nGiven = true; nGiven = true;
n = strtol(optarg,NULL,10); char* end;
n = strtol(optarg,&end,10);
if (end == optarg || *end != '\0' || n <= 0)
{
fprintf(stderr, "Invalid n value: %s\n", optarg);
return EXIT_FAILURE;
}
printf("n:%d\n",n); printf("n:%d\n",n);
// -n <Zahl> —MaximaleAnzahlanIterationen pro Bildpixel // -n <Zahl> —MaximaleAnzahlanIterationen pro Bildpixel
break; break;
}
case 'r': { case 'r': {
rGiven = true; rGiven = true;
char *end; char *end;
resolution = strtof(optarg,&end); resolution = strtof(optarg,&end);
if (end == optarg || *end != '\0') if (end == optarg || *end != '\0' || resolution <= 0.0f)
{ {
fprintf(stderr, "Invalid floating point number: %s\n",optarg); fprintf(stderr, "Invalid floating point number: %s\n",optarg);
return EXIT_FAILURE; return EXIT_FAILURE;
@@ -319,7 +334,19 @@ int main(int argc, char** argv)
case 'o': case 'o':
oGiven = true; oGiven = true;
if (optarg[0] == '-')
{
fprintf(stderr, "Invalid output filename: %s\n",optarg);
return EXIT_FAILURE;
}
output_data = malloc(strlen(optarg) + 1); output_data = malloc(strlen(optarg) + 1);
if (output_data == NULL)
{
fprintf(stderr,"Memory allocation failed.\n");
return EXIT_FAILURE;
}
strcpy(output_data,optarg); strcpy(output_data,optarg);
printf("output data: %s\n", output_data); // test printf("output data: %s\n", output_data); // test
// -o <Dateiname> —Ausgabedatei // -o <Dateiname> —Ausgabedatei
@@ -373,6 +400,8 @@ int main(int argc, char** argv)
if (!(nGiven && rGiven && oGiven && dGiven && sGiven)) if (!(nGiven && rGiven && oGiven && dGiven && sGiven))
{ {
checkInputs(nGiven,rGiven,oGiven,dGiven,sGiven); checkInputs(nGiven,rGiven,oGiven,dGiven,sGiven);
free(tempArr);
free(output_data);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@@ -382,7 +411,7 @@ int main(int argc, char** argv)
benchmarking(amount_function_called,version_to_use,complexA,width,height,resolution,n,colored,tempArr); benchmarking(amount_function_called,version_to_use,complexA,width,height,resolution,n,colored,tempArr);
free(tempArr); free(tempArr);
free(output_data); free(output_data);
return 0; return EXIT_SUCCESS;
} }
switch (version_to_use) switch (version_to_use)