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

Laszlo Papp djszapi2 at gmail.com
Sat Oct 24 10:35:42 EDT 2009


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

Signed-off-by: Laszlo Papp <djszapi at archlinux.us>
---
 lib/libalpm/backup.c     |    4 ++--
 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    |    2 +-
 lib/libalpm/sync.c       |   10 +++++-----
 lib/libalpm/util.c       |   10 +++++-----
 src/pacman/callback.c    |    2 +-
 src/pacman/conf.c        |    2 +-
 src/pacman/pacman.c      |   14 +++++++-------
 src/pacman/util.c        |    2 +-
 12 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/lib/libalpm/backup.c b/lib/libalpm/backup.c
index a0c6b7f..af9ec68 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,7 +53,7 @@ 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);
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..380e8b2 100644
--- a/lib/libalpm/package.c
+++ b/lib/libalpm/package.c
@@ -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 6b0cf34..6f4a941 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';
@@ -325,7 +325,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';
@@ -576,7 +576,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);
 		}
@@ -630,7 +630,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..889f875 100644
--- a/src/pacman/conf.c
+++ b/src/pacman/conf.c
@@ -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..bb6030b 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)++;
@@ -513,7 +513,7 @@ static int parseargs(int argc, char *argv[])
 				config->quiet = 1;
 				break;
 			case 'r':
-				config->rootdir = strdup(optarg);
+				config->rootdir = strndup(optarg, PATH_MAX);
 				break;
 			case 's':
 				config->op_s_search = 1;
@@ -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 9adc0e5..ec74c8c 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -136,7 +136,7 @@ char *mdirname(const char *path)
 		return(strdup("."));
 	}
 
-	ret = strdup(path);
+	ret = strndup(path, PATH_MAX);
 	last = strrchr(ret, '/');
 
 	if(last != NULL) {
-- 
1.6.5



More information about the pacman-dev mailing list