Using xstrndup() yields more compact and readable code than using xmalloc(), memcpy() and manual NUL termination. Thanks to Alex Riesen <raa.lkml@xxxxxxxxx> for suggesting this. Also fixes a buglet where item->keywords would always be set to "tag", even if item->tag was empty. Signed-off-by: Johan Herland <johan@xxxxxxxxxxx> --- On Saturday 09 June 2007, Alex Riesen wrote: > ... and what's wrong with strndup? Nothing. ...Johan tag.c | 27 ++++++++++----------------- 1 files changed, 10 insertions(+), 17 deletions(-) diff --git a/tag.c b/tag.c index c3a2855..2307ec9 100644 --- a/tag.c +++ b/tag.c @@ -219,26 +219,19 @@ int parse_and_verify_tag_buffer(struct tag *item, } if (item) { /* Store parsed information into item */ - if (tag_len) { /* optional tag name was given */ - item->tag = xmalloc(tag_len + 1); - memcpy(item->tag, tag_line, tag_len); - item->tag[tag_len] = '\0'; - } - else { /* optional tag name not given */ - item->tag = xmalloc(1); - item->tag[0] = '\0'; - } + if (tag_len) /* optional tag name was given */ + item->tag = xstrndup(tag_line, tag_len); + else /* optional tag name not given */ + item->tag = xstrndup("", 0); - if (keywords_len) { /* optional keywords string was given */ - item->keywords = xmalloc(keywords_len + 1); - memcpy(item->keywords, keywords_line, keywords_len); - item->keywords[keywords_len] = '\0'; - } + if (keywords_len) /* optional keywords string was given */ + item->keywords = xstrndup(keywords_line, keywords_len); else { /* optional keywords string not given. Set default */ /* if tag name is set, use "tag"; else use "note" */ - const char *default_kw = item->tag ? "tag" : "note"; - item->keywords = xmalloc(strlen(default_kw) + 1); - memcpy(item->keywords, default_kw, strlen(default_kw) + 1); + if (*(item->tag)) + item->keywords = xstrndup("tag", 3); + else + item->keywords = xstrndup("note", 4); } if (!strcmp(type, blob_type)) { -- 1.5.2.1.144.gabc40 - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html