]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Add Str_Strncpy
authorOliver Kurth <okurth@vmware.com>
Mon, 23 Oct 2017 21:21:19 +0000 (14:21 -0700)
committerOliver Kurth <okurth@vmware.com>
Mon, 23 Oct 2017 21:21:19 +0000 (14:21 -0700)
Add a Str_Strncpy function that, unlike strncpy, guarantees
NUL-termination.

open-vm-tools/lib/include/str.h
open-vm-tools/lib/string/str.c

index 3f3d24f298271b474e0308a326b37879763915c5..cf58054a7f5bf43f79b452aa7bc15f77bf8a9176 100644 (file)
@@ -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:
index c201bc6d73d4072e9bbd20956802fabcccaf91a0..974e2db14523f607afafb000d0ac1cd82bc74b5d 100644 (file)
@@ -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__);
    }
 
    /*