[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[groff] 77/78: [troff]: Skip allocation of zero-length arrays.
From: |
G. Branden Robinson |
Subject: |
[groff] 77/78: [troff]: Skip allocation of zero-length arrays. |
Date: |
Fri, 7 Apr 2023 12:18:00 -0400 (EDT) |
gbranden pushed a commit to branch branden-2023-04-07
in repository groff.
commit c09128d9ef8c8181e61614ae762308d2ac27b29f
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 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 840078e0a..fb3c2885a 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 2a86c9c01..5f110e0bc 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()
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [groff] 77/78: [troff]: Skip allocation of zero-length arrays.,
G. Branden Robinson <=