gbranden pushed a commit to branch branden-2023-07-05 in repository groff. commit 5bec16eb018e15822d4d7038fd1212ee735a02ec Author: G. Branden Robinson <g.branden.robin...@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 9f31a5151..87bd8fe11 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2023-04-03 G. Branden Robinson <g.branden.robin...@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.robin...@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() _______________________________________________ Groff-commit mailing list Groff-commit@gnu.org https://lists.gnu.org/mailman/listinfo/groff-commit