[glib: 6/7] Fix the math in copy_chars




commit 0908e6a8e7370be6ebb8caf79d8bfe9920cd8c11
Author: LRN <lrn1986 gmail com>
Date:   Sat May 15 04:21:37 2021 +0000

    Fix the math in copy_chars
    
    Now we end up returning a pointer to the end of the buffer
    after we run out of space. On subsequent calls copy_count will
    end up being 0.

 glib/gwin32.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
---
diff --git a/glib/gwin32.c b/glib/gwin32.c
index c710ec603..f4590916f 100644
--- a/glib/gwin32.c
+++ b/glib/gwin32.c
@@ -1054,16 +1054,10 @@ copy_chars (char       *buffer,
             gsize      *buffer_size,
             const char *to_copy)
 {
-  gsize copy_count = strlen (to_copy);
-  if (copy_count <= *buffer_size)
-    memset (buffer, 0x20, copy_count);
-  else
-    memset (buffer, 0x20, *buffer_size);
-  strncpy_s (buffer, *buffer_size, to_copy, copy_count);
-  if (*buffer_size >= copy_count)
-    *buffer_size -= copy_count;
-  else
-    *buffer_size = 0;
+  gsize copy_count = MIN (strlen (to_copy), *buffer_size - 1);
+  memset (buffer, 0x20, copy_count);
+  strncpy_s (buffer, *buffer_size, to_copy, _TRUNCATE);
+  *buffer_size -= copy_count;
   return &buffer[copy_count];
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]