1
0
Fork 0

(svn r26499) -Codechange: replace strndup with stredup

release/1.5
rubidium 2014-04-24 18:37:39 +00:00
parent 382ca0941f
commit 8960939b22
6 changed files with 30 additions and 43 deletions

View File

@ -1107,7 +1107,7 @@ void DetermineBasePaths(const char *exe)
* unvalidated data we rather not want internally. */ * unvalidated data we rather not want internally. */
const char *homedir = getenv("HOME"); const char *homedir = getenv("HOME");
if (homedir != NULL) { if (homedir != NULL) {
homedir = strndup(homedir, MAX_PATH); homedir = stredup(homedir);
} }
if (homedir == NULL) { if (homedir == NULL) {

View File

@ -66,7 +66,7 @@ void NORETURN CDECL strgen_fatal(const char *s, ...)
*/ */
LanguageStrings::LanguageStrings(const char *language, const char *end) 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. */ /** 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--; while (i > 0 && (buffer[i - 1] == '\r' || buffer[i - 1] == '\n' || buffer[i - 1] == ' ')) i--;
buffer[i] = '\0'; buffer[i] = '\0';
*ret->lines.Append() = strndup(buffer, to_read); *ret->lines.Append() = stredup(buffer, buffer + to_read - 1);
if (len > to_read) { if (len > to_read) {
to_read = 0; to_read = 0;

View File

@ -21,14 +21,12 @@
* Construct a new in-memory item of an Ini file. * Construct a new in-memory item of an Ini file.
* @param parent the group we belong to * @param parent the group we belong to
* @param name the name of the item * @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 = stredup(name, last);
str_validate(this->name, this->name + strlen(this->name));
this->name = strndup(name, len);
if (this->name != NULL) str_validate(this->name, this->name + len);
*parent->last_item = this; *parent->last_item = this;
parent->last_item = &this->next; 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. * Construct a new in-memory group of an Ini file.
* @param parent the file we belong to * @param parent the file we belong to
* @param name the name of the group * @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 = stredup(name, last);
str_validate(this->name, this->name + strlen(this->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->last_item = &this->item; this->last_item = &this->item;
*parent->last_group = this; *parent->last_group = this;
@ -116,7 +111,7 @@ IniItem *IniGroup::GetItem(const char *name, bool create)
if (!create) return NULL; if (!create) return NULL;
/* otherwise make a new one */ /* 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; if (!create_new) return NULL;
/* otherwise make a new one */ /* otherwise make a new one */
IniGroup *group = new IniGroup(this, name, len); IniGroup *group = new IniGroup(this, name, name + len);
group->comment = strdup("\n"); group->comment = strdup("\n");
return group; return group;
} }
@ -267,17 +262,17 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
e--; e--;
} }
s++; // skip [ s++; // skip [
group = new IniGroup(this, s, e - s); group = new IniGroup(this, s, e - 1);
if (comment_size != 0) { if (comment_size != 0) {
group->comment = strndup(comment, comment_size); group->comment = stredup(comment, comment + comment_size);
comment_size = 0; comment_size = 0;
} }
} else if (group != NULL) { } else if (group != NULL) {
if (group->type == IGT_SEQUENCE) { if (group->type == IGT_SEQUENCE) {
/* A sequence group, use the line as item name without further interpretation. */ /* 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) { if (comment_size) {
item->comment = strndup(comment, comment_size); item->comment = stredup(comment, comment + comment_size);
comment_size = 0; comment_size = 0;
} }
continue; continue;
@ -293,9 +288,9 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
} }
/* it's an item in an existing group */ /* 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) { if (comment_size != 0) {
item->comment = strndup(comment, comment_size); item->comment = stredup(comment, comment + comment_size);
comment_size = 0; comment_size = 0;
} }
@ -311,7 +306,7 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
*e = '\0'; *e = '\0';
/* If the value was not quoted and empty, it must be NULL */ /* 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)); if (item->value != NULL) str_validate(item->value, item->value + strlen(item->value));
} else { } else {
/* it's an orphan item */ /* it's an orphan item */
@ -320,7 +315,7 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
} }
if (comment_size > 0) { if (comment_size > 0) {
this->comment = strndup(comment, comment_size); this->comment = stredup(comment, comment + comment_size);
comment_size = 0; comment_size = 0;
} }

View File

@ -28,7 +28,7 @@ struct IniItem {
char *value; ///< The value of this item char *value; ///< The value of this item
char *comment; ///< The comment associated with 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(); ~IniItem();
void SetValue(const char *value); void SetValue(const char *value);
@ -43,7 +43,7 @@ struct IniGroup {
char *name; ///< name of group char *name; ///< name of group
char *comment; ///< comment for 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(); ~IniGroup();
IniItem *GetItem(const char *name, bool create); IniItem *GetItem(const char *name, bool create);

View File

@ -32,21 +32,19 @@
/* Use stredup instead. */ /* Use stredup instead. */
//#define strdup SAFEGUARD_DO_NOT_USE_THIS_METHOD //#define strdup SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define strndup SAFEGUARD_DO_NOT_USE_THIS_METHOD
/* Use stredup instead. */
//#define strndup SAFEGUARD_DO_NOT_USE_THIS_METHOD
/* Use strecpy instead. */ /* Use strecpy instead. */
//#define strcpy SAFEGUARD_DO_NOT_USE_THIS_METHOD //#define strcpy SAFEGUARD_DO_NOT_USE_THIS_METHOD
//#define strncpy SAFEGUARD_DO_NOT_USE_THIS_METHOD //#define strncpy SAFEGUARD_DO_NOT_USE_THIS_METHOD
/* Use strecat instead. */ /* Use strecat instead. */
//#define strcat SAFEGUARD_DO_NOT_USE_THIS_METHOD #define strcat SAFEGUARD_DO_NOT_USE_THIS_METHOD
//#define strncat SAFEGUARD_DO_NOT_USE_THIS_METHOD #define strncat SAFEGUARD_DO_NOT_USE_THIS_METHOD
/* Use seprintf instead. */ /* Use seprintf instead. */
//#define sprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD #define sprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
//#define snprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD #define snprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
/* Use vseprintf instead. */ /* Use vseprintf instead. */
//#define vsprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD //#define vsprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
@ -58,10 +56,4 @@
/* No clear replacement. */ /* No clear replacement. */
#define strtok SAFEGUARD_DO_NOT_USE_THIS_METHOD #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 */ #endif /* SAFEGUARDS_H */

View File

@ -1501,7 +1501,7 @@ static void AISaveConfig(IniFile *ini, const char *grpname)
name = "none"; name = "none";
} }
IniItem *item = new IniItem(group, name, strlen(name)); IniItem *item = new IniItem(group, name);
item->SetValue(value); item->SetValue(value);
} }
} }
@ -1524,7 +1524,7 @@ static void GameSaveConfig(IniFile *ini, const char *grpname)
name = "none"; name = "none";
} }
IniItem *item = new IniItem(group, name, strlen(name)); IniItem *item = new IniItem(group, name);
item->SetValue(value); item->SetValue(value);
} }