gbranden pushed a commit to branch master
in repository groff.

commit b9f1c82a68c12b443b45af62378d08b0753f11b8
Author: G. Branden Robinson <[email protected]>
AuthorDate: Fri Jun 19 00:38:16 2026 -0500

    src/libs/libgroff/string.cpp: Refactor.
    
    * src/libs/libgroff/string.cpp (string::operator=): assert(3) that the
      `p` parameter (a C string) is not a null pointer.  For `NDEBUG`
      builds, assign it an empty string literal in this situation.  Measure
      `p` with strlen(3) and call `sfree_alloc()` instead of `sfree()` on
      member variable `ptr` to size a buffer for it.  assert(3) that
      `sfree_alloc()` returns a non-null pointer.
    
      (sfree): Delete now-unused function.
---
 ChangeLog                    | 11 +++++++++++
 src/libs/libgroff/string.cpp | 36 +++++++++---------------------------
 2 files changed, 20 insertions(+), 27 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c870dfa5d..9ff0c060e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2026-06-19  G. Branden Robinson <[email protected]>
+
+       * src/libs/libgroff/string.cpp: Refactor.
+       (string::operator=): assert(3) that the `p` parameter (a C
+       string) is not a null pointer.  For `NDEBUG` builds, assign it
+       an empty string literal in this situation.  Measure `p` with
+       strlen(3) and call `sfree_alloc()` instead of `sfree()` on
+       member variable `ptr` to size a buffer for it.  assert(3) that
+       `sfree_alloc()` returns a non-null pointer.
+       (sfree): Delete now-unused function.
+
 2026-06-20  G. Branden Robinson <[email protected]>
 
        * src/libs/libgroff/string.cpp (string::clear): Maintain
diff --git a/src/libs/libgroff/string.cpp b/src/libs/libgroff/string.cpp
index 56c0d9497..13fcdb822 100644
--- a/src/libs/libgroff/string.cpp
+++ b/src/libs/libgroff/string.cpp
@@ -68,20 +68,6 @@ static char *salloc(size_t len, size_t *sizep)
   return p;
 }
 
-static void sfree(char *ptr)
-{
-  delete[] ptr;
-  size_t amount = initial_string_buffer_size;
-  try {
-    ptr = new char[amount];
-  }
-  catch (const std::bad_alloc &exc) {
-    fatal("cannot allocate %1 bytes for string reallocation after"
-         " freeing", amount);
-  }
-  memset(ptr, 0, amount);
-}
-
 static char *sfree_alloc(char *ptr, size_t oldsz, size_t len,
                         size_t *sizep)
 {
@@ -204,19 +190,15 @@ string &string::operator=(const string &s)
 
 string &string::operator=(const char *p)
 {
-  if (0 /* nullptr */ == p) {
-    sfree(ptr);
-    len = 0;
-    ptr = 0 /* nullptr */;
-    sz = 0;
-  }
-  else {
-    size_t slen = strlen(p);
-    ptr = sfree_alloc(ptr, sz, slen, &sz);
-    len = slen;
-    if (len != 0)
-      memcpy(ptr, p, len);
-  }
+  assert(p != 0 /* nullptr */);
+  if (0 /* nullptr */ == p)
+    p = "";
+  size_t slen = strlen(p);
+  ptr = sfree_alloc(ptr, sz, slen, &sz);
+  assert(ptr != 0 /* nullptr */);
+  len = slen;
+  if (len != 0)
+    memcpy(ptr, p, len);
   return *this;
 }
 

_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit

Reply via email to