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)
{
case 'V': // done, function needed
version_to_use = strtol(optarg,NULL,10);
if (version_to_use < 0 || version_to_use > 5)
char *end;
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);
return EXIT_FAILURE;
@@ -232,12 +235,14 @@ int main(int argc, char** argv)
// gesetzt wird, soll ebenfalls die Hauptimplementierung ausgeführt werden
break;
case 'B': // done, function needed
case 'B': { // done, function needed
// wird die Laufzeit der angegebenen Implementierung gemessen und ausgegeben (do this later)
amount_function_called = strtol(optarg,NULL,10);
if (amount_function_called <= 0) {
char *end;
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");
return 1;
return EXIT_FAILURE;
}
will_benchmark = true;
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 .
break;
}
case 's': {
sGiven = true;
@@ -296,18 +302,27 @@ int main(int argc, char** argv)
break;
}
case 'n':
case 'n': {
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);
// -n <Zahl> —MaximaleAnzahlanIterationen pro Bildpixel
break;
}
case 'r': {
rGiven = true;
char *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);
return EXIT_FAILURE;
@@ -319,7 +334,19 @@ int main(int argc, char** argv)
case 'o':
oGiven = true;
if (optarg[0] == '-')
{
fprintf(stderr, "Invalid output filename: %s\n",optarg);
return EXIT_FAILURE;
}
output_data = malloc(strlen(optarg) + 1);
if (output_data == NULL)
{
fprintf(stderr,"Memory allocation failed.\n");
return EXIT_FAILURE;
}
strcpy(output_data,optarg);
printf("output data: %s\n", output_data); // test
// -o <Dateiname> —Ausgabedatei
@@ -373,6 +400,8 @@ int main(int argc, char** argv)
if (!(nGiven && rGiven && oGiven && dGiven && sGiven))
{
checkInputs(nGiven,rGiven,oGiven,dGiven,sGiven);
free(tempArr);
free(output_data);
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);
free(tempArr);
free(output_data);
return 0;
return EXIT_SUCCESS;
}
switch (version_to_use)