gbranden pushed a commit to branch master
in repository groff.
commit c390daadefc502826338da055c7bd9ba9be79d1b
Author: G. Branden Robinson <[email protected]>
AuthorDate: Thu Jul 10 21:02:08 2025 -0500
[troff]: Catch `std::bad_alloc` exceptions (3/3).
...from `new` operator. Throw a fatal error indicating how much memory
we couldn't allocate.
* src/roff/troff/input.cpp (get_delimited_name): Do it.
Exhibit:
$ { printf '\\C@'; dd if=/dev/zero of=/dev/stdout bs=1M count=1024 \
| tr '\0' 'a'; printf '@\n'; } | ./build/test-groff
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 10.1333 s, 106 MB/s
troff:<standard input>:1: fatal error: cannot allocate 1073741824 bytes to
read input line
---
ChangeLog | 2 +-
src/roff/troff/input.cpp | 24 +++++++++++++++++++-----
2 files changed, 20 insertions(+), 6 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index d1f5698c6..d5beb1727 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,7 +5,7 @@
allocate.
* src/roff/troff/input.cpp (read_long_escape_parameters)
- (do_get_long_name): Do it.
+ (do_get_long_name, get_delimited_name): Do it.
2025-07-10 G. Branden Robinson <[email protected]>
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index c1d905a67..4bdebd4c5 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -5800,16 +5800,30 @@ static symbol get_delimited_name()
}
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;
for (;;) {
if ((i + 1) > 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;
}
tok.next();
_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit