diff options
author | Dan McGee <dan@archlinux.org> | 2011-07-05 21:16:17 +0200 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2011-07-06 04:29:02 +0200 |
commit | ae7139adcfa65991c71616e8de7910ff722d4166 (patch) | |
tree | 2126517e48edf71ae49e51fa264cc8ea4b946f81 /src/pacman/util.c | |
parent | dfc532668d4a4182ce196a895fdd5b017b505c6f (diff) | |
download | pacman-ae7139adcfa65991c71616e8de7910ff722d4166.tar.gz pacman-ae7139adcfa65991c71616e8de7910ff722d4166.tar.xz |
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>
Diffstat (limited to 'src/pacman/util.c')
-rw-r--r-- | src/pacman/util.c | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/src/pacman/util.c b/src/pacman/util.c index deb3e056..7065abdc 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -389,22 +389,21 @@ char *strreplace(const char *str, const char *needle, const char *replace) * x "size difference between replace and needle" */ newsz = strlen(str) + 1 + alpm_list_count(list) * (replacesz - needlesz); - newstr = malloc(newsz); + newstr = calloc(newsz, sizeof(char)); if(!newstr) { return NULL; } - *newstr = '\0'; p = str; newp = newstr; for(i = list; i; i = alpm_list_next(i)) { q = alpm_list_getdata(i); - if(q > p){ + if(q > p) { /* 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; } - strncpy(newp, replace, replacesz); + memcpy(newp, replace, replacesz); newp += replacesz; p = q + needlesz; } @@ -413,9 +412,7 @@ char *strreplace(const char *str, const char *needle, const char *replace) if(*p) { /* add the rest of 'p' */ strcpy(newp, p); - newp += strlen(p); } - *newp = '\0'; return newstr; } |