gbranden pushed a commit to branch master
in repository groff.
commit ad85cbebff0ec4f754120a10c1519046cf6f2758
Author: G. Branden Robinson <[email protected]>
AuthorDate: Sun May 31 21:26:06 2026 -0500
[grohtml]: Refactor. (2/2)
Be more fastidious with heap-allocated memory.
* src/devices/grohtml/output.cpp: Use ISO C++98 exceptions to handle
heap storage allocation failures. Preprocessor-include C++ "<new>"
header file.
(word::word): Slightly refactor. Introduce new local variable
`amount` to track the count of `char`s we wish to allocate, so that we
can use it both in a `new` allocation and in the fatal diagnostic
thrown if that allocation fails.
(word::word)
(word_list::add_word): Catch `std::bad_alloc` exception and `fatal()`
out with an attempt to describe what we were doing.
Continues the long process of fixing Savannah #68192.
---
ChangeLog | 17 +++++++++++++++++
src/devices/grohtml/output.cpp | 21 ++++++++++++++++++---
2 files changed, 35 insertions(+), 3 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 12129ea7e..c20e90982 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2026-05-31 G. Branden Robinson <[email protected]>
+
+ [grohtml]: Be more fastidious with heap-allocated memory.
+
+ * src/devices/grohtml/output.cpp: Use ISO C++98 exceptions to
+ handle heap storage allocation failures. Preprocessor-include
+ C++ "<new>" header file.
+ (word::word): Slightly refactor. Introduce new local variable
+ `amount` to track the count of `char`s we wish to allocate, so
+ that we can use it both in a `new` allocation and in the fatal
+ diagnostic thrown if that allocation fails.
+ (word::word):
+ (word_list::add_word): Catch `std::bad_alloc` exception and
+ `fatal()` out with an attempt to describe what we were doing.
+
+ Continues the long process of fixing Savannah #68192.
+
2026-05-31 G. Branden Robinson <[email protected]>
[grohtml]: Fix code style nit.
diff --git a/src/devices/grohtml/output.cpp b/src/devices/grohtml/output.cpp
index 61709668a..73b31e584 100644
--- a/src/devices/grohtml/output.cpp
+++ b/src/devices/grohtml/output.cpp
@@ -33,6 +33,8 @@ along with this program. If not, see
<http://www.gnu.org/licenses/>. */
// getc(), putc(), sprintf()
#include <string.h> // strlen(), strncpy()
+#include <new> // std::bad_alloc
+
#include "cset.h"
#include "driver.h"
#include "lib.h" // INT_DIGITS
@@ -66,7 +68,13 @@ along with this program. If not, see
<http://www.gnu.org/licenses/>. */
word::word (const char *w, int n)
: next(0 /* nullptr */)
{
- s = new char[n+1];
+ size_t amount = n + 1;
+ try {
+ s = new char[amount];
+ }
+ catch (const std::bad_alloc &exc) {
+ fatal("cannot allocate %1 bytes to copy word", amount);
+ }
strncpy(s, w, n);
s[n] = '\0';
}
@@ -120,11 +128,18 @@ int word_list::flush (FILE *f)
void word_list::add_word (const char *s, int n)
{
+ word *new_word = 0 /* nullptr */;
+ try {
+ new_word = new word(s, n);
+ }
+ catch (const std::bad_alloc &exc) {
+ fatal("cannot allocate %1 bytes to add word to word list", n);
+ }
if (head == 0 /* nullptr */) {
- head = new word(s, n);
+ head = new_word;
tail = head;
} else {
- tail->next = new word(s, n);
+ tail->next = new_word;
tail = tail->next;
}
length += n;
_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit