Remove most usages of strncmp()
The supposed safety blanket of this function is better handled by explicit length checking and usages of strlen() on known NULL-terminated strings rather than hoping things fit in a buffer. We also have no need to fully fill a PATH_MAX length variable with NULLs every time as long as a single terminating byte is there. Remove usages of it by using strcpy() or memcpy() as appropriate, after doing length checks via strlen(). Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
parent
dfc532668d
commit
ae7139adcf
|
@ -299,7 +299,7 @@ static char *canonicalize_path(const char *path) {
|
||||||
len += 1;
|
len += 1;
|
||||||
}
|
}
|
||||||
CALLOC(new_path, len + 1, sizeof(char), return NULL);
|
CALLOC(new_path, len + 1, sizeof(char), return NULL);
|
||||||
strncpy(new_path, path, len);
|
strcpy(new_path, path);
|
||||||
new_path[len - 1] = '/';
|
new_path[len - 1] = '/';
|
||||||
return new_path;
|
return new_path;
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,8 +110,7 @@ static int query_fileowner(alpm_list_t *targets)
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
char path[PATH_MAX];
|
char path[PATH_MAX];
|
||||||
const char *root;
|
const char *root;
|
||||||
char *append;
|
size_t rootlen;
|
||||||
size_t max_length;
|
|
||||||
alpm_list_t *t;
|
alpm_list_t *t;
|
||||||
alpm_db_t *db_local;
|
alpm_db_t *db_local;
|
||||||
|
|
||||||
|
@ -125,9 +124,13 @@ static int query_fileowner(alpm_list_t *targets)
|
||||||
* once, then we can just overwrite whatever file was there on the previous
|
* once, then we can just overwrite whatever file was there on the previous
|
||||||
* iteration. */
|
* iteration. */
|
||||||
root = alpm_option_get_root(config->handle);
|
root = alpm_option_get_root(config->handle);
|
||||||
strncpy(path, root, PATH_MAX - 1);
|
rootlen = strlen(root);
|
||||||
append = path + strlen(path);
|
if(rootlen + 1 > PATH_MAX) {
|
||||||
max_length = PATH_MAX - (append - path) - 1;
|
/* we are in trouble here */
|
||||||
|
pm_fprintf(stderr, ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, "");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
strcpy(path, root);
|
||||||
|
|
||||||
db_local = alpm_option_get_localdb(config->handle);
|
db_local = alpm_option_get_localdb(config->handle);
|
||||||
|
|
||||||
|
@ -208,11 +211,11 @@ static int query_fileowner(alpm_list_t *targets)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(strlen(pkgfile) > max_length) {
|
if(rootlen + 1 + strlen(pkgfile) > PATH_MAX) {
|
||||||
pm_fprintf(stderr, ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, pkgfile);
|
pm_fprintf(stderr, ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, pkgfile);
|
||||||
}
|
}
|
||||||
/* concatenate our file and the root path */
|
/* concatenate our file and the root path */
|
||||||
strcpy(append, pkgfile);
|
strcpy(path + rootlen, pkgfile);
|
||||||
|
|
||||||
pdname = mdirname(path);
|
pdname = mdirname(path);
|
||||||
ppath = resolve_path(pdname);
|
ppath = resolve_path(pdname);
|
||||||
|
|
|
@ -389,22 +389,21 @@ char *strreplace(const char *str, const char *needle, const char *replace)
|
||||||
* x "size difference between replace and needle" */
|
* x "size difference between replace and needle" */
|
||||||
newsz = strlen(str) + 1 +
|
newsz = strlen(str) + 1 +
|
||||||
alpm_list_count(list) * (replacesz - needlesz);
|
alpm_list_count(list) * (replacesz - needlesz);
|
||||||
newstr = malloc(newsz);
|
newstr = calloc(newsz, sizeof(char));
|
||||||
if(!newstr) {
|
if(!newstr) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
*newstr = '\0';
|
|
||||||
|
|
||||||
p = str;
|
p = str;
|
||||||
newp = newstr;
|
newp = newstr;
|
||||||
for(i = list; i; i = alpm_list_next(i)) {
|
for(i = list; i; i = alpm_list_next(i)) {
|
||||||
q = alpm_list_getdata(i);
|
q = alpm_list_getdata(i);
|
||||||
if(q > p){
|
if(q > p) {
|
||||||
/* add chars between this occurence and last occurence, if any */
|
/* add chars between this occurence and last occurence, if any */
|
||||||
strncpy(newp, p, (size_t)(q - p));
|
memcpy(newp, p, (size_t)(q - p));
|
||||||
newp += q - p;
|
newp += q - p;
|
||||||
}
|
}
|
||||||
strncpy(newp, replace, replacesz);
|
memcpy(newp, replace, replacesz);
|
||||||
newp += replacesz;
|
newp += replacesz;
|
||||||
p = q + needlesz;
|
p = q + needlesz;
|
||||||
}
|
}
|
||||||
|
@ -413,9 +412,7 @@ char *strreplace(const char *str, const char *needle, const char *replace)
|
||||||
if(*p) {
|
if(*p) {
|
||||||
/* add the rest of 'p' */
|
/* add the rest of 'p' */
|
||||||
strcpy(newp, p);
|
strcpy(newp, p);
|
||||||
newp += strlen(p);
|
|
||||||
}
|
}
|
||||||
*newp = '\0';
|
|
||||||
|
|
||||||
return newstr;
|
return newstr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h> /* printf */
|
#include <stdio.h> /* printf */
|
||||||
#include <string.h> /* strncpy */
|
#include <string.h>
|
||||||
|
|
||||||
#define BASENAME "vercmp"
|
#define BASENAME "vercmp"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue