Skip to content

Commit b6d0566

Browse files
committed
Plug memory leaks identified by fuzzer+Valgrind
1 parent 41ad163 commit b6d0566

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

crates/engine_xetex/xetex/xetex-ext.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ load_mapping_file(const char* s, const char* e, char byteMapping)
218218
font_mapping_warning(buffer, strlen(buffer), 2); /* not loadable */
219219
else if (get_tracing_fonts_state() > 1)
220220
font_mapping_warning(buffer, strlen(buffer), 0); /* tracing */
221+
222+
free(mapping);
221223
} else {
222224
font_mapping_warning(buffer, strlen(buffer), 1); /* not found */
223225
}

crates/engine_xetex/xetex/xetex-ini.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
Licensed under the MIT License.
44
*/
55

6+
/* On Windows this can bring in a `#define INPUT` that clashes with
7+
* xetex_format.h, so include it first and sanitize: */
8+
#include "teckit-c-Engine.h"
9+
#ifdef INPUT
10+
#undef INPUT
11+
#endif
12+
613
#include "xetex-core.h"
714
#include "xetex-xetexd.h"
815
#include "xetex-synctex.h"
@@ -2764,7 +2771,7 @@ load_fmt_file(void)
27642771

27652772
font_ptr = x;
27662773

2767-
font_mapping = xmalloc_array(void *, font_max);
2774+
font_mapping = xcalloc_array(void *, font_max);
27682775
font_layout_engine = xcalloc_array(void *, font_max);
27692776
font_flags = xmalloc_array(char, font_max);
27702777
font_letter_space = xmalloc_array(scaled_t, font_max);
@@ -3508,6 +3515,11 @@ tt_cleanup(void) {
35083515
release_font_engine(font_layout_engine[font_k], font_area[font_k]);
35093516
font_layout_engine[font_k] = NULL;
35103517
}
3518+
3519+
if (font_mapping[font_k] != NULL) {
3520+
TECkit_DisposeConverter((TECkit_Converter) font_mapping[font_k]);
3521+
font_mapping[font_k] = NULL;
3522+
}
35113523
}
35123524

35133525
for (int i = 1; i <= in_open; i++) {

crates/pdf_io/pdf_io/dpx-pdfdraw.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ typedef struct pdf_gstate_
878878
pdf_obj *extgstate;
879879
} pdf_gstate;
880880

881-
static dpx_stack gs_stack;
881+
static dpx_stack gs_stack = { 0, NULL, NULL };
882882

883883
static void
884884
init_a_gstate (pdf_gstate *gs)
@@ -1112,6 +1112,16 @@ pdf_dev_init_gstates (void)
11121112
{
11131113
pdf_gstate *gs;
11141114

1115+
/* Tectonic: this function is called twice in the xdvipdfmx driver init,
1116+
* resulting in a small amount of leaked memory. We statically initialize the
1117+
* stack variable to make it possible to safely avoid the leak in this
1118+
* situation. */
1119+
1120+
while ((gs = dpx_stack_pop(&gs_stack)) != NULL) {
1121+
clear_a_gstate(gs);
1122+
free(gs);
1123+
}
1124+
11151125
dpx_stack_init(&gs_stack);
11161126

11171127
gs = NEW(1, pdf_gstate);

fuzz/run-fuzzer.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
#!/usr/bin/env bash
2+
3+
# NOTE: install `llvm-dev` or whichever package provides `llvm-symbolizer` if
4+
# you want your stack traces to have any useful information! Otherwise none of
5+
# the binary addresses are decoded when the fuzzer finds problems. You may also
6+
# need to add the `-D` flag to `cargo fuzz run` to get more meaningful
7+
# backtraces, at the expense of the fuzzer running much slower.
8+
29
set -e
310
set -o pipefail
411
HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

0 commit comments

Comments
 (0)