groff-commit
[Top][All Lists]
Advanced

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

[groff] 80/127: [troff]: Skip allocation of zero-length arrays.


From: G. Branden Robinson
Subject: [groff] 80/127: [troff]: Skip allocation of zero-length arrays.
Date: Mon, 10 Jul 2023 04:30:58 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit 5a80c2412b89f64a31236ab2ee5719a94a6b01e3
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Mon Apr 3 19:30:56 2023 -0500

    [troff]: Skip allocation of zero-length arrays.
    
    * src/roff/troff/input.cpp (temp_iterator::temp_iterator): Skip
      allocation of zero-length arrays.  Resolves "-Walloc-zero" warning
      from GCC.
    
    Fixes <https://savannah.gnu.org/bugs/?62398>.  Thanks to Bjarni Ingi
    Gislason for the report.
    
    It is not necessary to make conditional the subsequent `delete[]` of a
    null pointer.  "If the _delete-expression_ calls the implementation
    deallocation function (3.7.3.2), and if the operand of the delete
    expression is not the null pointer constant, the deallocation function
    will deallocate the storage referenced by the pointer thus rendering the
    pointer invalid" (ISO/IEC 14882-1998, ยง5.3.5, paragraph 4).  Or as
    Stroustrup puts it, "Applying _delete_ to zero has no effect." (_The C++
    Programming Language, Special Edition_, p. 128).
    
    Also annotate some null pointers with `nullptr` comments to ease any
    future transition to C++11, which defines it as a keyword.
---
 ChangeLog                |  9 +++++++++
 src/roff/troff/input.cpp | 14 ++++++++------
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index cf2b7fd15..ee5ecc846 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2023-04-03  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/roff/troff/input.cpp (temp_iterator::temp_iterator):
+       Skip allocation of zero-length arrays.  Resolves "-Walloc-zero"
+       warning from GCC.
+
+       Fixes <https://savannah.gnu.org/bugs/?62398>.  Thanks to Bjarni
+       Ingi Gislason for the report.
+
 2023-04-02  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/roff/troff/input.cpp (token::description): Revise
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 54d8d271e..0c9986fd3 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -234,12 +234,12 @@ private:
 };
 
 input_iterator::input_iterator()
-: is_diversion(0), ptr(0), eptr(0)
+: is_diversion(0), ptr(0 /* nullptr */), eptr(0 /* nullptr */)
 {
 }
 
 input_iterator::input_iterator(int is_div)
-: is_diversion(is_div), ptr(0), eptr(0)
+: is_diversion(is_div), ptr(0 /* nullptr */), eptr(0 /* nullptr */)
 {
 }
 
@@ -3627,12 +3627,14 @@ public:
 inline
 #endif
 temp_iterator::temp_iterator(const char *s, int len)
+: base(0 /* nullptr */)
 {
-  base = new unsigned char[len];
-  if (len > 0)
+  if (len > 0) {
+    base = new unsigned char[len];
     memcpy(base, s, len);
-  ptr = base;
-  eptr = base + len;
+    ptr = base;
+    eptr = base + len;
+  }
 }
 
 temp_iterator::~temp_iterator()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]