ref: 5ccf89a00d7be33b1934d22a9eb8d82a2be153c4
parent: 85bcd1c917864673aa86cc14921e0ac0f209da9a
parent: 745e7342badc686f40ee25c2fa211e69924ad938
author: Turo Lamminen <[email protected]>
date: Fri Aug 27 17:39:25 EDT 2021
Merge pull request #1346 from turol/const Refactor myargv and fix const correctness issue
--- a/src/i_main.c
+++ b/src/i_main.c
@@ -18,6 +18,7 @@
#include "config.h"
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
@@ -26,7 +27,9 @@
#include "doomtype.h"
#include "i_system.h"
#include "m_argv.h"
+#include "m_misc.h"
+
//
// D_DoomMain()
// Not a globally visible function, just included for source reference,
@@ -40,7 +43,13 @@
// save arguments
myargc = argc;
- myargv = argv;
+ myargv = malloc(argc * sizeof(char *));
+ assert(myargv != NULL);
+
+ for (int i = 0; i < argc; i++)
+ {
+ myargv[i] = M_StringDuplicate(argv[i]);
+ }
//!
// Print the program version and exit.
--- a/src/m_argv.c
+++ b/src/m_argv.c
@@ -132,6 +132,7 @@
for (i=0; i<argv_index; ++i)
{
newargv[i] = myargv[i];
+ myargv[i] = NULL;
++newargc;
}
@@ -158,10 +159,11 @@
if (infile[k] == '\"')
{
+ char *argstart;
// Skip the first character(")
++k;
- newargv[newargc++] = &infile[k];
+ argstart = &infile[k];
// Read all characters between quotes
@@ -180,12 +182,14 @@
infile[k] = '\0';
++k;
+ newargv[newargc++] = M_StringDuplicate(argstart);
}
else
{
+ char *argstart;
// Read in the next argument until a space is reached
- newargv[newargc++] = &infile[k];
+ argstart = &infile[k];
while(k < size && !isspace(infile[k]))
{
@@ -197,6 +201,7 @@
infile[k] = '\0';
++k;
+ newargv[newargc++] = M_StringDuplicate(argstart);
}
}
@@ -205,12 +210,26 @@
for (i=argv_index + 1; i<myargc; ++i)
{
newargv[newargc] = myargv[i];
+ myargv[i] = NULL;
++newargc;
}
+ // Free any old strings in myargv which were not moved to newargv
+ for (i = 0; i < myargc; ++i)
+ {
+ if (myargv[i] != NULL)
+ {
+ free(myargv[i]);
+ myargv[i] = NULL;
+ }
+ }
+
+ free(myargv);
myargv = newargv;
myargc = newargc;
+ free(file);
+
#if 0
// Disabled - Vanilla Doom does not do this.
// Display arguments
@@ -259,7 +278,8 @@
// the loop we'll ignore it. Since some parameters stop reading when
// an argument beginning with a '-' is encountered, we keep something
// that starts with a '-'.
- myargv[i] = "-_";
+ free(myargv[i]);
+ myargv[i] = M_StringDuplicate("-_");
LoadResponseFile(i + 1, myargv[i + 1]);
}
}