gbranden pushed a commit to branch master
in repository groff.

commit c6b648fa2bb49911c1517cdd659aa403f4af451d
Author: G. Branden Robinson <[email protected]>
AuthorDate: Wed Jun 10 05:21:14 2026 -0500

    [libgroff]: Continue `string` refactoring.
    
    * src/libs/libgroff/string.cpp: Continue refactoring to implement new
      invariant: any existing `string` object is backed by "clean" storage.
    
      (sfree_alloc, srealloc): Simplify special-case handling of replacement
      string having zero length.  Deal with it as `salloc()` now does, using
      `initial_string_buffer_size` as the `amount` in that case.
---
 ChangeLog                    | 10 ++++++++++
 src/libs/libgroff/string.cpp | 45 +++++++++++++++++++-------------------------
 2 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index cf1054e5a..a037aa4ad 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2026-06-09  G. Branden Robinson <[email protected]>
+
+       * src/libs/libgroff/string.cpp: Continue refactoring to
+       implement new invariant: any existing `string` object is backed
+       by "clean" storage.
+       (sfree_alloc, srealloc): Simplify special-case handling of
+       replacement string having zero length.  Deal with it as
+       `salloc()` now does, using `initial_string_buffer_size` as the
+       `amount` in that case.
+
 2026-06-10  G. Branden Robinson <[email protected]>
 
        * src/libs/libgroff/string.cpp: Increase initial buffer size
diff --git a/src/libs/libgroff/string.cpp b/src/libs/libgroff/string.cpp
index 862586dad..44bcc3c42 100644
--- a/src/libs/libgroff/string.cpp
+++ b/src/libs/libgroff/string.cpp
@@ -91,10 +91,6 @@ static char *sfree_alloc(char *ptr, size_t oldsz, size_t len,
     return ptr;
   }
   delete[] ptr;
-  if (0 == len) {
-    *sizep = 0;
-    return 0 /* nullptr */; // XXX: GBR: breaks invariant
-  }
   char *p = 0 /* nullptr */;
   size_t amount = len;
   if (0 == amount)
@@ -117,30 +113,27 @@ static char *srealloc(char *ptr, size_t oldsz, size_t 
oldlen,
     *sizep = oldsz;
     return ptr;
   }
-  if (0 == newlen) {
-    delete[] ptr;
-    *sizep = 0;
-    return 0 /* nullptr */; // XXX: GBR: breaks invariant
+  size_t amount = newlen;
+  if (0 == amount)
+    amount = initial_string_buffer_size;
+  else
+    // If the string changes size once, assume it will change again, in
+    // an effort to avoid excessive reallocations.
+    amount = newlen * 2;
+  char *p = 0 /* nullptr */;
+  try {
+    p = new char[*sizep = amount];
   }
-  else {
-    // If the string changes size once, assume it will change again; try
-    // to prevent excessive reallocations.
-    size_t amount = newlen * 2;
-    char *p = 0 /* nullptr */;
-    try {
-      p = new char[*sizep = amount];
-    }
-    catch (const std::bad_alloc &exc) {
-      fatal("cannot allocate %1 bytes for string reallocation", amount);
-    }
-    if ((oldlen < newlen) && (oldlen != 0)) {
-      assert(amount > 0);
-      memset(p, 0, amount);
-      memcpy(p, ptr, oldlen);
-    }
-    delete[] ptr;
-    return p;
+  catch (const std::bad_alloc &exc) {
+    fatal("cannot allocate %1 bytes for string reallocation", amount);
+  }
+  if ((oldlen < newlen) && (oldlen != 0)) {
+    assert(amount > 0);
+    memset(p, 0, amount);
+    memcpy(p, ptr, oldlen);
   }
+  delete[] ptr;
+  return p;
 }
 
 string::string() : len(0), sz(initial_string_buffer_size)

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

Reply via email to