From: Oliver Kurth Date: Mon, 23 Oct 2017 21:21:19 +0000 (-0700) Subject: Add Str_Strncpy X-Git-Tag: stable-10.3.0~263 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2496fecbcd4c99bb3cc73df64b24982607f54227;p=thirdparty%2Fopen-vm-tools.git Add Str_Strncpy Add a Str_Strncpy function that, unlike strncpy, guarantees NUL-termination. --- diff --git a/open-vm-tools/lib/include/str.h b/open-vm-tools/lib/include/str.h index 3f3d24f29..cf58054a7 100644 --- a/open-vm-tools/lib/include/str.h +++ b/open-vm-tools/lib/include/str.h @@ -1,5 +1,5 @@ /********************************************************* - * Copyright (C) 1998-2016 VMware, Inc. All rights reserved. + * Copyright (C) 1998-2017 VMware, Inc. All rights reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published @@ -118,6 +118,10 @@ char *Str_Strnstr(const char *src, // IN: char *Str_Strcpy(char *dst, // OUT: const char *src, // IN: size_t maxLen); // IN: +char *Str_Strncpy(char *dest, // OUT: + size_t destSize, // IN: + const char *src, // IN: + size_t n); // IN: char *Str_Strcat(char *dst, // IN/OUT: const char *src, // IN: size_t maxLen); // IN: diff --git a/open-vm-tools/lib/string/str.c b/open-vm-tools/lib/string/str.c index c201bc6d7..974e2db14 100644 --- a/open-vm-tools/lib/string/str.c +++ b/open-vm-tools/lib/string/str.c @@ -301,6 +301,47 @@ Str_Strcpy(char *buf, // OUT } +/* + *----------------------------------------------------------------------------- + * + * Str_Strncpy -- + * + * Unlike strncpy: + * * Guaranteed to NUL-terminate. + * * If the src string is shorter than n bytes, does NOT zero-fill the + * remaining bytes. + * * Panics if a buffer overrun would have occurred. + * + * Results: + * Same as strncpy. + * + * Side effects: + * None. + * + *----------------------------------------------------------------------------- + */ + +char * +Str_Strncpy(char *dest, // IN/OUT + size_t destSize, // IN: Size of dest + const char *src, // IN: String to copy + size_t n) // IN: Max chars of src to copy, not including NUL +{ + ASSERT(dest != NULL); + ASSERT(src != NULL); + + n = Str_Strlen(src, n); + + if (n >= destSize) { + Panic("%s:%d Buffer too small\n", __FILE__, __LINE__); + } + + memcpy(dest, src, n); + dest[n] = '\0'; + return dest; +} + + /* *---------------------------------------------------------------------- * @@ -433,7 +474,7 @@ Str_Strcat(char *buf, // IN/OUT * Specifically, this function will Panic if a buffer overrun would * have occurred. * - * Guaranteed to NUL-terminate if bufSize > 0. + * Guaranteed to NUL-terminate. * * Results: * Same as strncat. @@ -468,7 +509,7 @@ Str_Strncat(char *buf, // IN/OUT if (!(bufLen + n < bufSize || bufLen + strlen(src) < bufSize)) { - Panic("%s:%d Buffer too small\n", __FILE__,__LINE__); + Panic("%s:%d Buffer too small\n", __FILE__, __LINE__); } /* @@ -942,7 +983,7 @@ Str_Wcsncat(wchar_t *buf, // IN/OUT if (bufLen + n >= bufSize && bufLen + wcslen(src) >= bufSize) { - Panic("%s:%d Buffer too small\n", __FILE__,__LINE__); + Panic("%s:%d Buffer too small\n", __FILE__, __LINE__); } /*