[pacman-dev] [PATCH] Change strdup function using for strndup

Laszlo Papp djszapi2 at gmail.com
Sun Oct 18 00:40:45 EDT 2009


	* Size examined str* function usage is a common coding practice,
	* 	because it's more safer to avoid breakage while using str* functions.

Signed-off-by: Laszlo Papp <djszapi at archlinux.us>
---

Is there any MAX value for these variables?
*grp, package name, version, arch, license, replaces, backup, opt.depends, conflict,
provide, xfercommand

 lib/libalpm/backup.c     |    6 ++--
 lib/libalpm/be_package.c |    6 ++--
 lib/libalpm/conflict.c   |    2 +-
 lib/libalpm/db.c         |    2 +-
 lib/libalpm/handle.c     |    2 +-
 lib/libalpm/package.c    |    6 ++--
 lib/libalpm/sync.c       |   10 ++++----
 lib/libalpm/util.c       |   14 +++++-----
 src/pacman/callback.c    |    2 +-
 src/pacman/conf.c        |    4 +-
 src/pacman/pacman.c      |   56 +++++++++++++++++++++++-----------------------
 src/pacman/util.c        |    6 ++--
 12 files changed, 58 insertions(+), 58 deletions(-)

diff --git a/lib/libalpm/backup.c b/lib/libalpm/backup.c
index a0c6b7f..741f763 100644
--- a/lib/libalpm/backup.c
+++ b/lib/libalpm/backup.c
@@ -35,7 +35,7 @@
 /* split a backup string "file\thash" into two strings : file and hash */
 static int backup_split(const char *string, char **file, char **hash)
 {
-	char *str = strdup(string);
+	char *str = strndup(string, PATH_MAX);
 	char *ptr;

 	/* tab delimiter */
@@ -53,10 +53,10 @@ static int backup_split(const char *string, char **file, char **hash)
 	ptr++;
 	/* now str points to the filename and ptr points to the hash */
 	if(file) {
-		*file = strdup(str);
+		*file = strndup(str, PATH_MAX);
 	}
 	if(hash) {
-		*hash = strdup(ptr);
+		*hash = strndup(ptr, PATH_MAX);
 	}
 	FREE(str);
 	return(1);
diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c
index c1a4343..63fdb8b 100644
--- a/lib/libalpm/be_package.c
+++ b/lib/libalpm/be_package.c
@@ -169,7 +169,7 @@ static pmpkg_t *pkg_load(const char *pkgfile, int full)
 		RET_ERR(PM_ERR_MEMORY, NULL);
 	}

-	newpkg->filename = strdup(pkgfile);
+	newpkg->filename = strndup(pkgfile, PATH_MAX);
 	newpkg->size = st.st_size;

 	/* If full is false, only read through the archive until we find our needed
@@ -202,7 +202,7 @@ static pmpkg_t *pkg_load(const char *pkgfile, int full)
 			 * already been handled (for future possibilities) */
 		} else {
 			/* Keep track of all files for filelist generation */
-			newpkg->files = alpm_list_add(newpkg->files, strdup(entry_name));
+			newpkg->files = alpm_list_add(newpkg->files, strndup(entry_name, PATH_MAX));
 		}

 		if(archive_read_data_skip(archive)) {
@@ -234,7 +234,7 @@ static pmpkg_t *pkg_load(const char *pkgfile, int full)

 	/* internal fields for package struct */
 	newpkg->origin = PKG_FROM_FILE;
-	newpkg->origin_data.file = strdup(pkgfile);
+	newpkg->origin_data.file = strndup(pkgfile, PATH_MAX);

 	if(full) {
 		/* "checking for conflicts" requires a sorted list, ensure that here */
diff --git a/lib/libalpm/conflict.c b/lib/libalpm/conflict.c
index e934c01..d063859 100644
--- a/lib/libalpm/conflict.c
+++ b/lib/libalpm/conflict.c
@@ -516,7 +516,7 @@ alpm_list_t *_alpm_db_find_fileconflicts(pmdb_t *db, pmtrans_t *trans,
 					/* skip removal of file, but not add. this will prevent a second
 					 * package from removing the file when it was already installed
 					 * by its new owner (whether the file is in backup array or not */
-					trans->skip_remove = alpm_list_add(trans->skip_remove, strdup(filestr));
+					trans->skip_remove = alpm_list_add(trans->skip_remove, strndup(filestr, PATH_MAX));
 					_alpm_log(PM_LOG_DEBUG, "file changed packages, adding to remove skiplist: %s\n", filestr);
 					resolved_conflict = 1;
 				}
diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c
index dca5452..7f44759 100644
--- a/lib/libalpm/db.c
+++ b/lib/libalpm/db.c
@@ -189,7 +189,7 @@ int SYMEXPORT alpm_db_setserver(pmdb_t *db, const char *url)
 		len = strlen(url);
 	}
 	if(len) {
-		newurl = strdup(url);
+		newurl = strndup(url, len);
 		/* strip the trailing slash if one exists */
 		if(newurl[len - 1] == '/') {
 			newurl[len - 1] = '\0';
diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c
index 5cbf363..9e28527 100644
--- a/lib/libalpm/handle.c
+++ b/lib/libalpm/handle.c
@@ -431,7 +431,7 @@ int SYMEXPORT alpm_option_set_logfile(const char *logfile)
 		return(-1);
 	}

-	handle->logfile = strdup(logfile);
+	handle->logfile = strndup(logfile, PATH_MAX);

 	/* free the old logfile path string, and close the stream so logaction
 	 * will reopen a new stream on the new logfile */
diff --git a/lib/libalpm/package.c b/lib/libalpm/package.c
index 572b863..658b737 100644
--- a/lib/libalpm/package.c
+++ b/lib/libalpm/package.c
@@ -618,8 +618,8 @@ int SYMEXPORT alpm_pkg_vercmp(const char *a, const char *b)
 	/* easy comparison to see if versions are identical */
 	if(strcmp(a, b) == 0) return(0);

-	str1 = strdup(a);
-	str2 = strdup(b);
+	str1 = strndup(a, PATH_MAX);
+	str2 = strndup(b, PATH_MAX);

 	one = str1;
 	two = str2;
@@ -805,7 +805,7 @@ pmpkg_t *_alpm_pkg_dup(pmpkg_t *pkg)
 	/* internal */
 	newpkg->origin = pkg->origin;
 	if(newpkg->origin == PKG_FROM_FILE) {
-		newpkg->origin_data.file = strdup(pkg->origin_data.file);
+		newpkg->origin_data.file = strndup(pkg->origin_data.file, PATH_MAX);
 	} else {
 		newpkg->origin_data.db = pkg->origin_data.db;
 	}
diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c
index 2cdcd47..e16374c 100644
--- a/lib/libalpm/sync.c
+++ b/lib/libalpm/sync.c
@@ -830,7 +830,7 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data)

 						if(d->download_size != 0) {
 							/* add the delta filename to the download list if needed */
-							files = alpm_list_add(files, strdup(d->delta));
+							files = alpm_list_add(files, strndup(d->delta, PATH_MAX));
 						}

 						/* keep a list of all the delta files for md5sums */
@@ -841,7 +841,7 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data)
 					/* not using deltas */
 					if(spkg->download_size != 0) {
 						/* add the filename to the download list if needed */
-						files = alpm_list_add(files, strdup(fname));
+						files = alpm_list_add(files, strndup(fname, PATH_MAX));
 					}
 				}

@@ -881,7 +881,7 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data)

 			if(test_md5sum(trans, filename, md5sum) != 0) {
 				errors++;
-				*data = alpm_list_add(*data, strdup(filename));
+				*data = alpm_list_add(*data, strndup(filename, PATH_MAX));
 			}
 		}
 		if(errors) {
@@ -916,7 +916,7 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data)

 		if(test_md5sum(trans, filename, md5sum) != 0) {
 			errors++;
-			*data = alpm_list_add(*data, strdup(filename));
+			*data = alpm_list_add(*data, strndup(filename, PATH_MAX));
 			continue;
 		}
 		/* load the package file and replace pkgcache entry with it in the target list */
@@ -927,7 +927,7 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data)
 		if(alpm_pkg_load(filepath, 1, &pkgfile) != 0) {
 			_alpm_pkg_free(pkgfile);
 			errors++;
-			*data = alpm_list_add(*data, strdup(filename));
+			*data = alpm_list_add(*data, strndup(filename, PATH_MAX));
 			FREE(filepath);
 			continue;
 		}
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index d910809..906cb03 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -97,7 +97,7 @@ int _alpm_makepath_mode(const char *path, mode_t mode)
 	mode_t oldmask = umask(0000);
 	int ret = 0;

-	orig = strdup(path);
+	orig = strndup(path, PATH_MAX);
 	incr = calloc(strlen(orig) + 1, sizeof(char));
 	str = orig;
 	while((ptr = strsep(&str, "/"))) {
@@ -204,7 +204,7 @@ int _alpm_lckmk()
 	const char *file = alpm_option_get_lockfile();

 	/* create the dir of the lockfile first */
-	dir = strdup(file);
+	dir = strndup(file, PATH_MAX);
 	ptr = strrchr(dir, '/');
 	if(ptr) {
 		*ptr = '\0';
@@ -315,7 +315,7 @@ int _alpm_unpack(const char *archive, const char *prefix, alpm_list_t *list, int

 		st = archive_entry_stat(entry);
 		entryname = archive_entry_pathname(entry);
-
+
 		if(S_ISREG(st->st_mode)) {
 			archive_entry_set_perm(entry, 0644);
 		} else if(S_ISDIR(st->st_mode)) {
@@ -324,7 +324,7 @@ int _alpm_unpack(const char *archive, const char *prefix, alpm_list_t *list, int

 		/* If specific files were requested, skip entries that don't match. */
 		if(list) {
-			char *prefix = strdup(entryname);
+			char *prefix = strndup(entryname, PATH_MAX);
 			char *p = strstr(prefix,"/");
 			if(p) {
 				*(p+1) = '\0';
@@ -575,7 +575,7 @@ char *_alpm_filecache_find(const char* filename)
 		snprintf(path, PATH_MAX, "%s%s", (char*)alpm_list_getdata(i),
 				filename);
 		if(access(path, R_OK) == 0) {
-			retpath = strdup(path);
+			retpath = strndup(path, PATH_MAX);
 			_alpm_log(PM_LOG_DEBUG, "found cached pkg: %s\n", retpath);
 			return(retpath);
 		}
@@ -612,7 +612,7 @@ const char *_alpm_filecache_setup(void)
 	}

 	/* we didn't find a valid cache directory. use /tmp. */
-	tmp = alpm_list_add(NULL, strdup("/tmp/"));
+	tmp = alpm_list_add(NULL, strndup("/tmp/", PATH_MAX));
 	alpm_option_set_cachedirs(tmp);
 	_alpm_log(PM_LOG_DEBUG, "using cachedir: %s", "/tmp/\n");
 	_alpm_log(PM_LOG_WARNING, _("couldn't create package cache, using /tmp instead\n"));
@@ -629,7 +629,7 @@ const char *_alpm_filecache_setup(void)
 int _alpm_lstat(const char *path, struct stat *buf)
 {
 	int ret;
-	char *newpath = strdup(path);
+	char *newpath = strndup(path, PATH_MAX);
 	int len = strlen(newpath);

 	/* strip the trailing slash if one exists */
diff --git a/src/pacman/callback.c b/src/pacman/callback.c
index 858bfdf..f6444ed 100644
--- a/src/pacman/callback.c
+++ b/src/pacman/callback.c
@@ -553,7 +553,7 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
 	eta_m = eta_s / 60;
 	eta_s -= eta_m * 60;

-	fname = strdup(filename);
+	fname = strndup(filename, PATH_MAX);
 	/* strip package or DB extension for cleaner look */
 	if((p = strstr(fname, PKGEXT)) || (p = strstr(fname, DBEXT))) {
 			*p = '\0';
diff --git a/src/pacman/conf.c b/src/pacman/conf.c
index 92c6f4e..d118dd0 100644
--- a/src/pacman/conf.c
+++ b/src/pacman/conf.c
@@ -22,7 +22,7 @@

 #include <stdlib.h>
 #include <stdio.h>
-#include <string.h> /* strdup */
+#include <string.h> /* strndup */

 /* pacman */
 #include "conf.h"
@@ -44,7 +44,7 @@ config_t *config_new(void)
 	newconfig->op = PM_OP_MAIN;
 	newconfig->logmask = PM_LOG_ERROR | PM_LOG_WARNING;
 	/* CONFFILE is defined at compile-time */
-	newconfig->configfile = strdup(CONFFILE);
+	newconfig->configfile = strndup(CONFFILE, PATH_MAX);

 	return(newconfig);
 }
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index f4f8044..cd6dc9f 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -295,12 +295,12 @@ static void setlibpaths(void)
 			if(!config->dbpath) {
 				/* omit leading slash from our static DBPATH, root handles it */
 				snprintf(path, PATH_MAX, "%s%s", alpm_option_get_root(), DBPATH + 1);
-				config->dbpath = strdup(path);
+				config->dbpath = strndup(path, PATH_MAX);
 			}
 			if(!config->logfile) {
 				/* omit leading slash from our static LOGFILE path, root handles it */
 				snprintf(path, PATH_MAX, "%s%s", alpm_option_get_root(), LOGFILE + 1);
-				config->logfile = strdup(path);
+				config->logfile = strndup(path, PATH_MAX);
 			}
 		}
 		/* Set other paths if they were configured. Note that unless rootdir
@@ -477,7 +477,7 @@ static int parseargs(int argc, char *argv[])
 			case 'U': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_UPGRADE); break;
 			case 'V': config->version = 1; break;
 			case 'b':
-				config->dbpath = strdup(optarg);
+				config->dbpath = strndup(optarg, PATH_MAX);
 				break;
 			case 'c':
 				(config->op_s_clean)++;
@@ -494,26 +494,26 @@ static int parseargs(int argc, char *argv[])
 			case 'f': config->flags |= PM_TRANS_FLAG_FORCE; break;
 			case 'g': (config->group)++; break;
 			case 'h': config->help = 1; break;
-			case 'i': (config->op_q_info)++; (config->op_s_info)++; break;
-			case 'k':
-				config->flags |= PM_TRANS_FLAG_DBONLY;
-				config->op_q_check = 1;
-				break;
-			case 'l': config->op_q_list = 1; break;
-			case 'm': config->op_q_foreign = 1; break;
-			case 'n': config->flags |= PM_TRANS_FLAG_NOSAVE; break;
-			case 'o': config->op_q_owns = 1; break;
-			case 'p':
-				config->op_q_isfile = 1;
-				config->op_s_printuris = 1;
-				config->flags |= PM_TRANS_FLAG_NOCONFLICTS;
-				config->flags |= PM_TRANS_FLAG_NOLOCK;
-				break;
-			case 'q':
-				config->quiet = 1;
-				break;
-			case 'r':
-				config->rootdir = strdup(optarg);
+		case 'i': (config->op_q_info)++; (config->op_s_info)++; break;
+		case 'k':
+			config->flags |= PM_TRANS_FLAG_DBONLY;
+			config->op_q_check = 1;
+			break;
+		case 'l': config->op_q_list = 1; break;
+		case 'm': config->op_q_foreign = 1; break;
+		case 'n': config->flags |= PM_TRANS_FLAG_NOSAVE; break;
+		case 'o': config->op_q_owns = 1; break;
+		case 'p':
+			config->op_q_isfile = 1;
+			config->op_s_printuris = 1;
+			config->flags |= PM_TRANS_FLAG_NOCONFLICTS;
+			config->flags |= PM_TRANS_FLAG_NOLOCK;
+			break;
+		case 'q':
+			config->quiet = 1;
+			break;
+		case 'r':
+			config->rootdir = strndup(optarg, PATH_MAX);
 				break;
 			case 's':
 				config->op_s_search = 1;
@@ -723,7 +723,7 @@ static int _parseconfig(const char *file, const char *givensection,

 	/* if we are passed a section, use it as our starting point */
 	if(givensection != NULL) {
-		section = strdup(givensection);
+		section = strndup(givensection, PATH_MAX);
 	}
 	/* if we are passed a db, use it as our starting point */
 	if(givendb != NULL) {
@@ -749,7 +749,7 @@ static int _parseconfig(const char *file, const char *givensection,
 			if(section) {
 				free(section);
 			}
-			section = strdup(ptr);
+			section = strndup(ptr, PATH_MAX);
 			section[strlen(section)-1] = '\0';
 			pm_printf(PM_LOG_DEBUG, "config: new section '%s'\n", section);
 			if(!strlen(section)) {
@@ -840,7 +840,7 @@ static int _parseconfig(const char *file, const char *givensection,
 					} else if(strcmp(key, "DBPath") == 0) {
 						/* don't overwrite a path specified on the command line */
 						if(!config->dbpath) {
-							config->dbpath = strdup(ptr);
+							config->dbpath = strndup(ptr, PATH_MAX);
 							pm_printf(PM_LOG_DEBUG, "config: dbpath: %s\n", ptr);
 						}
 					} else if(strcmp(key, "CacheDir") == 0) {
@@ -854,12 +854,12 @@ static int _parseconfig(const char *file, const char *givensection,
 					} else if(strcmp(key, "RootDir") == 0) {
 						/* don't overwrite a path specified on the command line */
 						if(!config->rootdir) {
-							config->rootdir = strdup(ptr);
+							config->rootdir = strndup(ptr, PATH_MAX);
 							pm_printf(PM_LOG_DEBUG, "config: rootdir: %s\n", ptr);
 						}
 					} else if (strcmp(key, "LogFile") == 0) {
 						if(!config->logfile) {
-							config->logfile = strdup(ptr);
+							config->logfile = strndup(ptr, PATH_MAX);
 							pm_printf(PM_LOG_DEBUG, "config: logfile: %s\n", ptr);
 						}
 					} else if (strcmp(key, "XferCommand") == 0) {
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 0e5e7f5..fc8822f 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -176,10 +176,10 @@ char *mdirname(const char *path)

 	/* null or empty path */
 	if(path == NULL || path == '\0') {
-		return(strdup("."));
+		return(strndup(".", PATH_MAX));
 	}

-	ret = strdup(path);
+	ret = strndup(path, PATH_MAX);
 	last = strrchr(ret, '/');

 	if(last != NULL) {
@@ -189,7 +189,7 @@ char *mdirname(const char *path)
 	}
 	/* no slash found */
 	free(ret);
-	return(strdup("."));
+	return(strndup(".", PATH_MAX));
 }

 /* output a string, but wrap words properly with a specified indentation
--
1.6.4.4



More information about the pacman-dev mailing list