(svn r26499) -Codechange: replace strndup with stredup

replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
rubidium 10 years ago
parent 382ca0941f
commit 8960939b22

@ -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) {

@ -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;

@ -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;
}

@ -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);

@ -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 */

@ -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);
}

Loading…
Cancel
Save