Changeset - r21400:0108b5ae8ebe
[Not reviewed]
master
0 6 0
rubidium - 10 years ago 2014-04-24 18:37:39
rubidium@openttd.org
(svn r26499) -Codechange: replace strndup with stredup
6 files changed with 30 insertions and 43 deletions:
0 comments (0 inline, 0 general)
src/fileio.cpp
Show inline comments
 
@@ -1104,13 +1104,13 @@ void DetermineBasePaths(const char *exe)
 
	/* getenv is highly unsafe; duplicate it as soon as possible,
 
	 * or at least before something else touches the environment
 
	 * variables in any way. It can also contain all kinds of
 
	 * 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) {
 
		const struct passwd *pw = getpwuid(getuid());
 
		homedir = (pw == NULL) ? NULL : strdup(pw->pw_dir);
 
	}
src/game/game_text.cpp
Show inline comments
 
@@ -63,13 +63,13 @@ void NORETURN CDECL strgen_fatal(const c
 
 * Create a new container for language strings.
 
 * @param language The language name.
 
 * @param end If not NULL, terminate \a language at this position.
 
 */
 
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. */
 
LanguageStrings::~LanguageStrings()
 
{
 
	free(this->language);
 
@@ -112,13 +112,13 @@ LanguageStrings *ReadRawLanguageStrings(
 

	
 
			/* Remove trailing spaces/newlines from the string. */
 
			size_t i = len;
 
			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;
 
			} else {
 
				to_read -= len;
 
			}
src/ini_load.cpp
Show inline comments
 
@@ -18,20 +18,18 @@
 
#include "safeguards.h"
 

	
 
/**
 
 * 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;
 
}
 

	
 
/** Free everything we loaded. */
 
@@ -55,21 +53,18 @@ 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;
 
	parent->last_group = &this->next;
 

	
 
	if (parent->list_group_names != NULL) {
 
@@ -113,13 +108,13 @@ IniItem *IniGroup::GetItem(const char *n
 
		if (strcmp(item->name, name) == 0) return item;
 
	}
 

	
 
	if (!create) return NULL;
 

	
 
	/* otherwise make a new one */
 
	return new IniItem(this, name, strlen(name));
 
	return new IniItem(this, name, NULL);
 
}
 

	
 
/**
 
 * Clear all items in the group
 
 */
 
void IniGroup::Clear()
 
@@ -169,13 +164,13 @@ IniGroup *IniLoadFile::GetGroup(const ch
 
		}
 
	}
 

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

	
 
/**
 
 * Remove the group with the given name.
 
@@ -264,23 +259,23 @@ void IniLoadFile::LoadFromDisk(const cha
 
			if (e[-1] != ']') {
 
				this->ReportFileError("ini: invalid group name '", buffer, "'");
 
			} else {
 
				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;
 
			}
 
			char *t;
 
			/* find end of keyname */
 
@@ -290,15 +285,15 @@ void IniLoadFile::LoadFromDisk(const cha
 
				if (*t == '\"') *t = ' ';
 
			} else {
 
				for (t = s; *t != '\0' && *t != '=' && *t != '\t' && *t != ' '; t++) {}
 
			}
 

	
 
			/* 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;
 
			}
 

	
 
			/* find start of parameter */
 
			while (*t == '=' || *t == ' ' || *t == '\t') t++;
 

	
 
@@ -308,22 +303,22 @@ void IniLoadFile::LoadFromDisk(const cha
 
			/* remove ending quotation marks */
 
			e = t + strlen(t);
 
			if (e > t && e[-1] == '\"') e--;
 
			*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 */
 
			this->ReportFileError("ini: '", buffer, "' outside of group");
 
		}
 
	}
 

	
 
	if (comment_size > 0) {
 
		this->comment = strndup(comment, comment_size);
 
		this->comment = stredup(comment, comment + comment_size);
 
		comment_size = 0;
 
	}
 

	
 
	free(comment);
 
	fclose(in);
 
}
src/ini_type.h
Show inline comments
 
@@ -25,13 +25,13 @@ enum IniGroupType {
 
struct IniItem {
 
	IniItem *next; ///< The next item in this group
 
	char *name;    ///< The name of this item
 
	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);
 
};
 

	
 
/** A group within an ini file. */
 
@@ -40,13 +40,13 @@ struct IniGroup {
 
	IniGroupType type;   ///< type of group
 
	IniItem *item;       ///< the first item in the group
 
	IniItem **last_item; ///< the last item in the group
 
	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);
 
	void Clear();
 
};
 

	
src/safeguards.h
Show inline comments
 
@@ -29,39 +29,31 @@
 

	
 
/* Use ReallocT instead. */
 
#define realloc   SAFEGUARD_DO_NOT_USE_THIS_METHOD
 

	
 
/* 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
 
//#define vsnprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
 

	
 
/* Use fgets instead. */
 
#define gets      SAFEGUARD_DO_NOT_USE_THIS_METHOD
 

	
 
/* 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 */
src/settings.cpp
Show inline comments
 
@@ -1498,13 +1498,13 @@ static void AISaveConfig(IniFile *ini, c
 
		if (config->HasScript()) {
 
			name = config->GetName();
 
		} else {
 
			name = "none";
 
		}
 

	
 
		IniItem *item = new IniItem(group, name, strlen(name));
 
		IniItem *item = new IniItem(group, name);
 
		item->SetValue(value);
 
	}
 
}
 

	
 
static void GameSaveConfig(IniFile *ini, const char *grpname)
 
{
 
@@ -1521,13 +1521,13 @@ static void GameSaveConfig(IniFile *ini,
 
	if (config->HasScript()) {
 
		name = config->GetName();
 
	} else {
 
		name = "none";
 
	}
 

	
 
	IniItem *item = new IniItem(group, name, strlen(name));
 
	IniItem *item = new IniItem(group, name);
 
	item->SetValue(value);
 
}
 

	
 
/**
 
 * Save the version of OpenTTD to the ini file.
 
 * @param ini the ini to write to
0 comments (0 inline, 0 general)