summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/fileio.cpp2
-rw-r--r--src/game/game_text.cpp4
-rw-r--r--src/ini_load.cpp41
-rw-r--r--src/ini_type.h4
-rw-r--r--src/safeguards.h18
-rw-r--r--src/settings.cpp4
6 files changed, 30 insertions, 43 deletions
diff --git a/src/fileio.cpp b/src/fileio.cpp
index da49f47e5..49da7b9b4 100644
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -1107,7 +1107,7 @@ void DetermineBasePaths(const char *exe)
* unvalidated data we rather not want internally. */
const char *homedir = getenv("HOME");
if (homedir != NULL) {
- homedir = strndup(homedir, MAX_PATH);
+ homedir = stredup(homedir);
}
if (homedir == NULL) {
diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp
index 502869249..bfff460ae 100644
--- a/src/game/game_text.cpp
+++ b/src/game/game_text.cpp
@@ -66,7 +66,7 @@ void NORETURN CDECL strgen_fatal(const char *s, ...)
*/
LanguageStrings::LanguageStrings(const char *language, const char *end)
{
- this->language = end == NULL ? strdup(language) : strndup(language, end - language);
+ this->language = stredup(language, end - 1);
}
/** Free everything. */
@@ -115,7 +115,7 @@ LanguageStrings *ReadRawLanguageStrings(const char *file)
while (i > 0 && (buffer[i - 1] == '\r' || buffer[i - 1] == '\n' || buffer[i - 1] == ' ')) i--;
buffer[i] = '\0';
- *ret->lines.Append() = strndup(buffer, to_read);
+ *ret->lines.Append() = stredup(buffer, buffer + to_read - 1);
if (len > to_read) {
to_read = 0;
diff --git a/src/ini_load.cpp b/src/ini_load.cpp
index a64ca454c..8816f66c4 100644
--- a/src/ini_load.cpp
+++ b/src/ini_load.cpp
@@ -21,14 +21,12 @@
* Construct a new in-memory item of an Ini file.
* @param parent the group we belong to
* @param name the name of the item
- * @param len the length of the name of the item
+ * @param last the last element of the name of the item
*/
-IniItem::IniItem(IniGroup *parent, const char *name, size_t len) : next(NULL), value(NULL), comment(NULL)
+IniItem::IniItem(IniGroup *parent, const char *name, const char *last) : next(NULL), value(NULL), comment(NULL)
{
- if (len == 0) len = strlen(name);
-
- this->name = strndup(name, len);
- if (this->name != NULL) str_validate(this->name, this->name + len);
+ this->name = stredup(name, last);
+ str_validate(this->name, this->name + strlen(this->name));
*parent->last_item = this;
parent->last_item = &this->next;
@@ -58,15 +56,12 @@ void IniItem::SetValue(const char *value)
* Construct a new in-memory group of an Ini file.
* @param parent the file we belong to
* @param name the name of the group
- * @param len the length of the name of the group
+ * @param last the last element of the name of the group
*/
-IniGroup::IniGroup(IniLoadFile *parent, const char *name, size_t len) : next(NULL), type(IGT_VARIABLES), item(NULL), comment(NULL)
+IniGroup::IniGroup(IniLoadFile *parent, const char *name, const char *last) : next(NULL), type(IGT_VARIABLES), item(NULL), comment(NULL)
{
- if (len == 0) len = strlen(name);
-
- this->name = strndup(name, len);
- if (this->name == NULL) error("not enough memory to allocate group name");
- str_validate(this->name, this->name + len);
+ this->name = stredup(name, last);
+ str_validate(this->name, this->name + strlen(this->name));
this->last_item = &this->item;
*parent->last_group = this;
@@ -116,7 +111,7 @@ IniItem *IniGroup::GetItem(const char *name, bool create)
if (!create) return NULL;
/* otherwise make a new one */
- return new IniItem(this, name, strlen(name));
+ return new IniItem(this, name, NULL);
}
/**
@@ -172,7 +167,7 @@ IniGroup *IniLoadFile::GetGroup(const char *name, size_t len, bool create_new)
if (!create_new) return NULL;
/* otherwise make a new one */
- IniGroup *group = new IniGroup(this, name, len);
+ IniGroup *group = new IniGroup(this, name, name + len);
group->comment = strdup("\n");
return group;
}
@@ -267,17 +262,17 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
e--;
}
s++; // skip [
- group = new IniGroup(this, s, e - s);
+ group = new IniGroup(this, s, e - 1);
if (comment_size != 0) {
- group->comment = strndup(comment, comment_size);
+ group->comment = stredup(comment, comment + comment_size);
comment_size = 0;
}
} else if (group != NULL) {
if (group->type == IGT_SEQUENCE) {
/* A sequence group, use the line as item name without further interpretation. */
- IniItem *item = new IniItem(group, buffer, e - buffer);
+ IniItem *item = new IniItem(group, buffer, e - 1);
if (comment_size) {
- item->comment = strndup(comment, comment_size);
+ item->comment = stredup(comment, comment + comment_size);
comment_size = 0;
}
continue;
@@ -293,9 +288,9 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
}
/* it's an item in an existing group */
- IniItem *item = new IniItem(group, s, t - s);
+ IniItem *item = new IniItem(group, s, t - 1);
if (comment_size != 0) {
- item->comment = strndup(comment, comment_size);
+ item->comment = stredup(comment, comment + comment_size);
comment_size = 0;
}
@@ -311,7 +306,7 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
*e = '\0';
/* If the value was not quoted and empty, it must be NULL */
- item->value = (!quoted && e == t) ? NULL : strndup(t, e - t);
+ item->value = (!quoted && e == t) ? NULL : stredup(t, e);
if (item->value != NULL) str_validate(item->value, item->value + strlen(item->value));
} else {
/* it's an orphan item */
@@ -320,7 +315,7 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
}
if (comment_size > 0) {
- this->comment = strndup(comment, comment_size);
+ this->comment = stredup(comment, comment + comment_size);
comment_size = 0;
}
diff --git a/src/ini_type.h b/src/ini_type.h
index 05133c77f..ce383b959 100644
--- a/src/ini_type.h
+++ b/src/ini_type.h
@@ -28,7 +28,7 @@ struct IniItem {
char *value; ///< The value of this item
char *comment; ///< The comment associated with this item
- IniItem(struct IniGroup *parent, const char *name, size_t len = 0);
+ IniItem(struct IniGroup *parent, const char *name, const char *last = NULL);
~IniItem();
void SetValue(const char *value);
@@ -43,7 +43,7 @@ struct IniGroup {
char *name; ///< name of group
char *comment; ///< comment for group
- IniGroup(struct IniLoadFile *parent, const char *name, size_t len = 0);
+ IniGroup(struct IniLoadFile *parent, const char *name, const char *last = NULL);
~IniGroup();
IniItem *GetItem(const char *name, bool create);
diff --git a/src/safeguards.h b/src/safeguards.h
index 993b44b06..8117eabf2 100644
--- a/src/safeguards.h
+++ b/src/safeguards.h
@@ -32,21 +32,19 @@
/* Use stredup instead. */
//#define strdup SAFEGUARD_DO_NOT_USE_THIS_METHOD
-
-/* Use stredup instead. */
-//#define strndup SAFEGUARD_DO_NOT_USE_THIS_METHOD
+#define strndup SAFEGUARD_DO_NOT_USE_THIS_METHOD
/* Use strecpy instead. */
//#define strcpy SAFEGUARD_DO_NOT_USE_THIS_METHOD
//#define strncpy SAFEGUARD_DO_NOT_USE_THIS_METHOD
/* Use strecat instead. */
-//#define strcat SAFEGUARD_DO_NOT_USE_THIS_METHOD
-//#define strncat SAFEGUARD_DO_NOT_USE_THIS_METHOD
+#define strcat SAFEGUARD_DO_NOT_USE_THIS_METHOD
+#define strncat SAFEGUARD_DO_NOT_USE_THIS_METHOD
/* Use seprintf instead. */
-//#define sprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
-//#define snprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
+#define sprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
+#define snprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
/* Use vseprintf instead. */
//#define vsprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
@@ -58,10 +56,4 @@
/* No clear replacement. */
#define strtok SAFEGUARD_DO_NOT_USE_THIS_METHOD
-/*
- * Possible future methods to mark unsafe, though needs more thought:
- * - memcpy; when memory area overlaps it messes up, use memmove.
- * - strlen: when the data is 'garbage', this could read beyond bounds.
- */
-
#endif /* SAFEGUARDS_H */
diff --git a/src/settings.cpp b/src/settings.cpp
index 4cb323eba..a5de03d21 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -1501,7 +1501,7 @@ static void AISaveConfig(IniFile *ini, const char *grpname)
name = "none";
}
- IniItem *item = new IniItem(group, name, strlen(name));
+ IniItem *item = new IniItem(group, name);
item->SetValue(value);
}
}
@@ -1524,7 +1524,7 @@ static void GameSaveConfig(IniFile *ini, const char *grpname)
name = "none";
}
- IniItem *item = new IniItem(group, name, strlen(name));
+ IniItem *item = new IniItem(group, name);
item->SetValue(value);
}