[pacman-dev] [PATCH 1/2] be_package: be more explicit parsing key/value pairs
This eliminates the need for strtrim() usage completely, instead relying on the fact that the only allowed delimiter between key and value is the " = " string. Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/be_package.c | 21 ++++++++++++--------- 1 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c index 41ecc75..c602996 100644 --- a/lib/libalpm/be_package.c +++ b/lib/libalpm/be_package.c @@ -171,18 +171,21 @@ static int parse_descfile(alpm_handle_t *handle, struct archive *a, alpm_pkg_t * size_t len = _alpm_strip_newline(buf.line); linenum++; - if(len == 0 || buf.line[0] == '#') { + key = buf.line; + if(len == 0 || key[0] == '#') { continue; } - ptr = buf.line; - key = strsep(&ptr, "="); - if(key == NULL || ptr == NULL) { - _alpm_log(handle, ALPM_LOG_DEBUG, "%s: syntax error in description file line %d\n", - newpkg->name ? newpkg->name : "error", linenum); + /* line is always in this format: "key = value" + * we can be sure the " = " exists, so look for that */ + ptr = memchr(key, ' ', len); + if(!ptr || ptr - key + 2 > len || memcmp(ptr, " = ", 3) != 0) { + _alpm_log(handle, ALPM_LOG_DEBUG, + "%s: syntax error in description file line %d\n", + newpkg->name ? newpkg->name : "error", linenum); } else { - key = _alpm_strtrim(key); - while(*ptr == ' ') ptr++; - ptr = _alpm_strtrim(ptr); + /* NULL the end of the key portion, move ptr to start of value */ + *ptr = '\0'; + ptr += 3; if(strcmp(key, "pkgname") == 0) { STRDUP(newpkg->name, ptr, return -1); newpkg->name_hash = _alpm_hash_sdbm(newpkg->name); -- 1.7.8.1
The last user of this was the code in the backend for loading packages, but this no longer uses it. Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/util.c | 41 ----------------------------------------- lib/libalpm/util.h | 1 - 2 files changed, 0 insertions(+), 42 deletions(-) diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 2e3f765..fc0e056 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -188,47 +188,6 @@ cleanup: return ret; } -/** Trim leading and trailing whitespace, including newlines, from a string. - * Modifies str in place. - * @param str a string to trim - * @return str - */ -char *_alpm_strtrim(char *str) -{ - char *pch = str; - - if(*str == '\0') { - /* string is empty, so we're done. */ - return str; - } - - while(isspace((unsigned char)*pch)) { - pch++; - } - if(pch != str) { - size_t len = strlen(pch); - if(len) { - /* move the remaining string to the beginning of str */ - memmove(str, pch, len + 1); - } else { - *str = '\0'; - } - } - - /* check if there wasn't anything but whitespace in the string. */ - if(*str == '\0') { - return str; - } - - pch = (str + (strlen(str) - 1)); - while(isspace((unsigned char)*pch)) { - pch--; - } - *++pch = '\0'; - - return str; -} - /** Trim trailing newlines from a string (if any exist). * @param str a single line of text * @return the length of the trimmed string diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index 54e69c1..6d5c0c3 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -113,7 +113,6 @@ enum _alpm_csum { int _alpm_makepath(const char *path); int _alpm_makepath_mode(const char *path, mode_t mode); int _alpm_copyfile(const char *src, const char *dest); -char *_alpm_strtrim(char *str); size_t _alpm_strip_newline(char *str); int _alpm_open_archive(alpm_handle_t *handle, const char *path, -- 1.7.8.1
On 24/12/11 06:40, Dan McGee wrote:
This eliminates the need for strtrim() usage completely, instead relying on the fact that the only allowed delimiter between key and value is the " = " string.
Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/be_package.c | 21 ++++++++++++--------- 1 files changed, 12 insertions(+), 9 deletions(-)
diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c index 41ecc75..c602996 100644 --- a/lib/libalpm/be_package.c +++ b/lib/libalpm/be_package.c @@ -171,18 +171,21 @@ static int parse_descfile(alpm_handle_t *handle, struct archive *a, alpm_pkg_t * size_t len = _alpm_strip_newline(buf.line);
linenum++; - if(len == 0 || buf.line[0] == '#') { + key = buf.line; + if(len == 0 || key[0] == '#') { continue; } - ptr = buf.line; - key = strsep(&ptr, "="); - if(key == NULL || ptr == NULL) { - _alpm_log(handle, ALPM_LOG_DEBUG, "%s: syntax error in description file line %d\n", - newpkg->name ? newpkg->name : "error", linenum); + /* line is always in this format: "key = value" + * we can be sure the " = " exists, so look for that */ + ptr = memchr(key, ' ', len); + if(!ptr || ptr - key + 2 > len || memcmp(ptr, " = ", 3) != 0) {
be_package.c: In function 'parse_descfile': be_package.c:181:28: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare] ptr - key + 2 is guaranteed to be > 0 so we can cast: if(!ptr || (size_t)(ptr - key + 2) > len || memcmp(ptr, " = ", 3) != 0) {
participants (2)
-
Allan McRae
-
Dan McGee