summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2011-08-25 17:14:19 -0500
committerDan McGee <dan@archlinux.org>2011-08-25 17:14:19 -0500
commitc5982a3eb5287afdec6c9b859f81795c773da8fe (patch)
treea7c560c2e6552a0344998766ca966ca740298915
parente1dce078b247142f7db80bf2d338e431df34691e (diff)
downloadpacman-c5982a3eb5287afdec6c9b859f81795c773da8fe.tar.xz
strtrim: don't move empty string
There were many cases where the string coming in was a blank line, e.g. "\n\0", length 1. The trim routine starts by trimming leading spaces, thus trimming everything. We would then proceed to do a memmove of the NULL byte, which is completely worthless as we can just assign it instead. Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r--lib/libalpm/util.c7
-rw-r--r--src/pacman/util.c7
-rw-r--r--src/util/pactree.c7
3 files changed, 18 insertions, 3 deletions
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 7e3bc372..cf783893 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -192,7 +192,12 @@ char *_alpm_strtrim(char *str)
pch++;
}
if(pch != str) {
- memmove(str, pch, (strlen(pch) + 1));
+ size_t len = strlen(pch);
+ if(len) {
+ memmove(str, pch, len + 1);
+ } else {
+ *str = '\0';
+ }
}
/* check if there wasn't anything but whitespace in the string. */
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 2de4bd44..cb62ec66 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -342,7 +342,12 @@ char *strtrim(char *str)
pch++;
}
if(pch != str) {
- memmove(str, pch, (strlen(pch) + 1));
+ size_t len = strlen(pch);
+ if(len) {
+ memmove(str, pch, len + 1);
+ } else {
+ *str = '\0';
+ }
}
/* check if there wasn't anything but whitespace in the string. */
diff --git a/src/util/pactree.c b/src/util/pactree.c
index 5a27cc77..b8832912 100644
--- a/src/util/pactree.c
+++ b/src/util/pactree.c
@@ -103,7 +103,12 @@ static char *strtrim(char *str)
pch++;
}
if(pch != str) {
- memmove(str, pch, (strlen(pch) + 1));
+ size_t len = strlen(pch);
+ if(len) {
+ memmove(str, pch, len + 1);
+ } else {
+ *str = '\0';
+ }
}
/* check if there wasn't anything but whitespace in the string. */