allow NULL values in setMainValue(),
authorborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sun, 30 Mar 2003 19:48:03 +0000 (19:48 +0000)
committerborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sun, 30 Mar 2003 19:48:03 +0000 (19:48 +0000)
removed compiler warning in SDCCsnprintf(),
small optimization of strncpyz()

git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2444 4a8a32a2-be11-0410-ad9d-d568d2c75423

src/SDCCutil.c

index cd5d5b36b61d5ba9e6bc9a03323c8214ae75ab20..bce624fc482104e60254fd3350c728457578620f 100644 (file)
@@ -267,7 +267,6 @@ void
 setMainValue (const char *pname, const char *pvalue)
 {
   assert(pname);
-  assert(pvalue);
 
   shash_add (&_mainValues, pname, pvalue);
 }
@@ -319,15 +318,16 @@ getRuntimeVariables(void)
 char *strncpyz(char *dest, const char *src, size_t n)
 {
     assert(n > 0);
-    
+
+    --n;
     // paranoia...
-    if (strlen(src) >= n)
+    if (strlen(src) > n)
     {
        fprintf(stderr, "strncpyz prevented buffer overrun!\n");
     }
     
     strncpy(dest, src, n);
-    dest[n - 1] = 0;
+    dest[n] = 0;
     return dest;
 }
 
@@ -360,30 +360,29 @@ char *strncatz(char *dest, const char *src, size_t n)
 #if defined(HAVE_VSNPRINTF) || defined(HAVE_VSPRINTF)
 size_t SDCCsnprintf(char *dst, size_t n, const char *fmt, ...)
 {
-    va_list args;
-    int  len;
-    
-    va_start(args, fmt);
-    
-# if defined(HAVE_VSNPRINTF)    
-    len = vsnprintf(dst, n, fmt, args);
+  va_list args;
+  int len;
+
+  va_start(args, fmt);
+
+# if defined(HAVE_VSNPRINTF)
+  len = vsnprintf(dst, n, fmt, args);
 # else
-    vsprintf(dst, fmt, args);
-    len = strlen(dst) + 1;
-# endif    
-    
-    va_end(args);
-    
-    /* on some gnu systems, vsnprintf returns -1 if output is truncated.
-     * In the C99 spec, vsnprintf returns the number of characters that 
-     * would have been written, were space available.
-     */
-    if ((len < 0) || len >= n)
-    {
-       fprintf(stderr, "internal error: sprintf truncated.\n");
-    }
-    
-    return len;
+  vsprintf(dst, fmt, args);
+  len = strlen(dst) + 1;
+# endif
+
+  va_end(args);
+
+  /* on some gnu systems, vsnprintf returns -1 if output is truncated.
+   * In the C99 spec, vsnprintf returns the number of characters that 
+   * would have been written, were space available.
+   */
+  if ((len < 0) || (size_t) len >= n) {
+    fprintf(stderr, "internal error: sprintf truncated.\n");
+  }
+
+  return len;
 }
 
 #endif