gbranden pushed a commit to branch master
in repository groff.
commit 21c8b8e7497e9e4c3511baf7eb55525fba0241d0
Author: G. Branden Robinson <[email protected]>
AuthorDate: Thu Jul 10 20:58:38 2025 -0500
[troff]: Catch `std::bad_alloc` exceptions (1/3).
...from `new` operator. Throw a fatal error indicating how much memory
we couldn't allocate.
* src/roff/troff/input.cpp (read_long_escape_parameters): Do it.
Exhibit:
$ { printf '\\['; dd if=/dev/zero of=/dev/stdout bs=1M count=1024 \
| tr '\0' 'a'; printf ']\n'; } | ./build/test-groff -z
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 12.3334 s, 87.1 MB/s
troff:<standard input>:1: fatal error: cannot allocate 1073741824 bytes to
read input line
---
ChangeLog | 8 ++++++++
src/roff/troff/input.cpp | 24 +++++++++++++++++++-----
2 files changed, 27 insertions(+), 5 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index fd18e1893..8f3261114 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2025-07-10 G. Branden Robinson <[email protected]>
+
+ [troff]: Catch `std::bad_alloc` exceptions from `new` operator.
+ Throw a fatal error indicating how much memory we couldn't
+ allocate.
+
+ * src/roff/troff/input.cpp (read_long_escape_parameters): Do it.
+
2025-07-10 G. Branden Robinson <[email protected]>
* src/roff/troff/input.cpp: Refactor to simplify. As an
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 4275e9f87..5245ef72e 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -996,7 +996,15 @@ static symbol read_long_escape_parameters(read_mode mode)
{
int start_level = input_stack::get_level();
int buf_size = default_buffer_size;
- char *buf = new char[buf_size];
+ char *buf = 0 /* nullptr */;
+ try {
+ // C++03: new char[buf_size]();
+ buf = new char[buf_size];
+ }
+ catch (const std::bad_alloc &e) {
+ fatal("cannot allocate %1 bytes to read input line", buf_size);
+ }
+ (void) memset(buf, 0, (buf_size * sizeof(char)));
int i = 0;
char c;
bool have_char = false;
@@ -1011,11 +1019,17 @@ static symbol read_long_escape_parameters(read_mode
mode)
break;
if (i + 2 > buf_size) {
char *old_buf = buf;
- // C++03: new char[buf_size * 2]();
- buf = new char[buf_size * 2];
- (void) memset(buf, 0, (buf_size * 2 * sizeof(char)));
+ int new_buf_size = buf_size * 2;
+ // C++03: new char[new_buf_size]();
+ try {
+ buf = new char[new_buf_size];
+ }
+ catch (const std::bad_alloc &e) {
+ fatal("cannot allocate %1 bytes to read input line", buf_size);
+ }
+ (void) memset(buf, 0, (new_buf_size * sizeof(char)));
memcpy(buf, old_buf, buf_size);
- buf_size *= 2;
+ buf_size = new_buf_size;
delete[] old_buf;
}
if ((']' == c) && (input_stack::get_level() == start_level))
_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit