gbranden pushed a commit to branch master
in repository groff.

commit f9f582418007ed6620db39bb0db59cdf1f1486d8
Author: G. Branden Robinson <[email protected]>
AuthorDate: Thu Jun 25 18:03:25 2026 -0500

    [libgroff]: Refactor `string::clear()`.
    
    * src/libs/libgroff/string.cpp (string::clear): Refactor to reduce
      unnecessary memory writes.
---
 ChangeLog                    | 5 +++++
 src/libs/libgroff/string.cpp | 5 ++++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 8a0d590fc..49141be22 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2026-06-25  G. Branden Robinson <[email protected]>
+
+       * src/libs/libgroff/string.cpp (string::clear): Refactor to
+       reduce unnecessary memory writes.
+
 2026-06-26  G. Branden Robinson <[email protected]>
 
        * src/libs/libgroff/string.cpp (string::move): Refactor to
diff --git a/src/libs/libgroff/string.cpp b/src/libs/libgroff/string.cpp
index 67fc36205..620d5ff11 100644
--- a/src/libs/libgroff/string.cpp
+++ b/src/libs/libgroff/string.cpp
@@ -337,7 +337,10 @@ void string::clear()
 {
   assert(ptr != 0 /* nullptr */);
   if (ptr != 0 /* nullptr */)
-    memset(ptr, 0, sz);
+    // Zero out only the existing `len`gth of the string plus a null
+    // terminator.  Any remaining storage (`sz - len`) should be zero
+    // zero already from prior allocations.
+    memset(ptr, 0, ((len < sz) ? (len + 1) : sz));
   else
     ptr = salloc(0, &sz); // unreachable unless `NDEBUG`
   len = 0;

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

Reply via email to