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
 
@@ -1098,25 +1098,25 @@ void DetermineBasePaths(const char *exe)
 
#else
 
#ifdef __HAIKU__
 
	BPath path;
 
	find_directory(B_USER_SETTINGS_DIRECTORY, &path);
 
	const char *homedir = strdup(path.Path());
 
#else
 
	/* 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);
 
	}
 
#endif
 

	
 
	if (homedir != NULL) {
 
		ValidateString(homedir);
 
		seprintf(tmp, lastof(tmp), "%s" PATHSEP "%s", homedir, PERSONAL_DIR);
 
		AppendPathSeparator(tmp, lastof(tmp));
src/game/game_text.cpp
Show inline comments
 
@@ -57,25 +57,25 @@ void NORETURN CDECL strgen_fatal(const c
 
	va_end(va);
 
	DEBUG(script, 0, "%s:%d: FATAL: %s", _file, _cur_line, buf);
 
	throw std::exception();
 
}
 

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

	
 
/**
 
 * Read all the raw language strings from the given file.
 
 * @param file The file to read from.
 
 * @return The raw strings, or NULL upon error.
 
@@ -106,25 +106,25 @@ LanguageStrings *ReadRawLanguageStrings(
 

	
 
		ret = new LanguageStrings(langname, strchr(langname, '.'));
 

	
 
		char buffer[2048];
 
		while (to_read != 0 && fgets(buffer, sizeof(buffer), fh) != NULL) {
 
			size_t len = strlen(buffer);
 

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

	
 
		fclose(fh);
 
		return ret;
 
	} catch (...) {
 
		if (fh != NULL) fclose(fh);
src/ini_load.cpp
Show inline comments
 
@@ -12,32 +12,30 @@
 
#include "stdafx.h"
 
#include "core/alloc_func.hpp"
 
#include "core/mem_func.hpp"
 
#include "ini_type.h"
 
#include "string_func.h"
 

	
 
#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. */
 
IniItem::~IniItem()
 
{
 
	free(this->name);
 
	free(this->value);
 
	free(this->comment);
 

	
 
@@ -49,33 +47,30 @@ IniItem::~IniItem()
 
 * @param value the value to replace with.
 
 */
 
void IniItem::SetValue(const char *value)
 
{
 
	free(this->value);
 
	this->value = strdup(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) {
 
		for (uint i = 0; parent->list_group_names[i] != NULL; i++) {
 
			if (strcmp(this->name, parent->list_group_names[i]) == 0) {
 
				this->type = IGT_LIST;
 
				return;
 
			}
 
		}
 
@@ -107,25 +102,25 @@ IniGroup::~IniGroup()
 
 * @param create whether to create an item when not found or not.
 
 * @return the requested item or NULL if not found.
 
 */
 
IniItem *IniGroup::GetItem(const char *name, bool create)
 
{
 
	for (IniItem *item = this->item; item != NULL; item = item->next) {
 
		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()
 
{
 
	delete this->item;
 
	this->item = NULL;
 
	this->last_item = &this->item;
 
}
 

	
 
@@ -163,25 +158,25 @@ IniGroup *IniLoadFile::GetGroup(const ch
 
	if (len == 0) len = strlen(name);
 

	
 
	/* does it exist already? */
 
	for (IniGroup *group = this->group; group != NULL; group = group->next) {
 
		if (!strncmp(group->name, name, len) && group->name[len] == 0) {
 
			return group;
 
		}
 
	}
 

	
 
	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.
 
 * @param name name of the group to remove.
 
 */
 
void IniLoadFile::RemoveGroup(const char *name)
 
{
 
	size_t len = strlen(name);
 
	IniGroup *prev = NULL;
 
@@ -258,73 +253,73 @@ void IniLoadFile::LoadFromDisk(const cha
 
			memcpy(comment + pos, s, e - s); // copy comment contents
 
			continue;
 
		}
 

	
 
		/* it's a group? */
 
		if (s[0] == '[') {
 
			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 */
 
			if (*s == '\"') {
 
				s++;
 
				for (t = s; *t != '\0' && *t != '\"'; t++) {}
 
				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++;
 

	
 
			bool quoted = (*t == '\"');
 
			/* remove starting quotation marks */
 
			if (*t == '\"') t++;
 
			/* 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
 
@@ -19,40 +19,40 @@ enum IniGroupType {
 
	IGT_VARIABLES = 0, ///< Values of the form "landscape = hilly".
 
	IGT_LIST      = 1, ///< A list of values, separated by \n and terminated by the next group block.
 
	IGT_SEQUENCE  = 2, ///< A list of uninterpreted lines, terminated by the next group block.
 
};
 

	
 
/** A single "line" in an ini file. */
 
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. */
 
struct IniGroup {
 
	IniGroup *next;      ///< the next group within this file
 
	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();
 
};
 

	
 
/** Ini file that only supports loading. */
 
struct IniLoadFile {
 
	IniGroup *group;                      ///< the first group in the ini
 
	IniGroup **last_group;                ///< the last group in the ini
 
	char *comment;                        ///< last comment in file
 
	const char * const *list_group_names; ///< NULL terminated list with group names that are lists
src/safeguards.h
Show inline comments
 
@@ -23,45 +23,37 @@
 

	
 
/* Use MallocT instead. */
 
#define malloc    SAFEGUARD_DO_NOT_USE_THIS_METHOD
 

	
 
/* Use MallocT instead. */
 
#define calloc    SAFEGUARD_DO_NOT_USE_THIS_METHOD
 

	
 
/* 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
 
@@ -1492,48 +1492,48 @@ static void AISaveConfig(IniFile *ini, c
 
	for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
 
		AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
 
		const char *name;
 
		char value[1024];
 
		config->SettingsToString(value, lastof(value));
 

	
 
		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)
 
{
 
	IniGroup *group = ini->GetGroup(grpname);
 

	
 
	if (group == NULL) return;
 
	group->Clear();
 

	
 
	GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
 
	const char *name;
 
	char value[1024];
 
	config->SettingsToString(value, lastof(value));
 

	
 
	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
 
 */
 
static void SaveVersionInConfig(IniFile *ini)
 
{
 
	IniGroup *group = ini->GetGroup("version");
 

	
 
	char version[9];
0 comments (0 inline, 0 general)