From 0b322d9635196147295807a190d07fde072cc70c Mon Sep 17 00:00:00 2001 From: Cyrill Gorcunov Date: Mon, 10 May 2010 14:18:17 +0400 Subject: [PATCH] kvm: Use strlcat helper for copying cmdline params Signed-off-by: Cyrill Gorcunov --- include/kvm/util.h | 2 ++ main.c | 2 +- util.c | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/include/kvm/util.h b/include/kvm/util.h index 5d818c8..fbc58f9 100644 --- a/include/kvm/util.h +++ b/include/kvm/util.h @@ -44,4 +44,6 @@ do { \ __stringify(cnd) "\n"); \ } while (0) +extern size_t strlcat(char *dest, const char *src, size_t count); + #endif /* KVM__UTIL_H */ diff --git a/main.c b/main.c index 917b14e..a6f2889 100644 --- a/main.c +++ b/main.c @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) strcpy(real_cmdline, "notsc nolapic nosmp noacpi earlyprintk=serial,keep"); if (kernel_cmdline) { - strncat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); + strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); real_cmdline[sizeof(real_cmdline)-1] = '\0'; } diff --git a/util.c b/util.c index 096e942..cf9340f 100644 --- a/util.c +++ b/util.c @@ -74,3 +74,28 @@ void die_perror(const char *s) perror(s); exit(1); } + +/** + * strlcat - Append a length-limited, %NUL-terminated string to another + * @dest: The string to be appended to + * @src: The string to append to it + * @count: The size of the destination buffer. + */ +size_t strlcat(char *dest, const char *src, size_t count) +{ + size_t dsize = strlen(dest); + size_t len = strlen(src); + size_t res = dsize + len; + + DIE_IF(dsize >= count); + + dest += dsize; + count -= dsize; + if (len >= count) + len = count - 1; + + memcpy(dest, src, len); + dest[len] = 0; + + return res; +}