1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
From: Andrej Shadura <andrew@shadura.me>
Subject: Restore the ROUND_UP macro and adjust the initial buffer size.
Bug-Debian: https://bugs.debian.org/1036851
When need_chars was moved from "handletoken.h" to "handletoken.c",
the ROUND_UP macro was removed, but the replacement was incorrect.
This caused the program to exit with a "Virtual memory exhausted"
error when it tried to reallocate 0 bytes (thus freeing the memory).
It reallocated to 0 bytes because the initial buffer size was less
than 1024, and the size calculation rounds down instead of up.
Bug: #56644
Fixes: c89d32a
---
src/handletoken.c | 2 +-
src/indent.h | 8 ++++++++
src/parse.c | 2 +-
3 files changed, 10 insertions(+), 2 deletions(-)
--- a/src/handletoken.c
+++ b/src/handletoken.c
@@ -85,7 +85,7 @@
if (current_size + needed >= (size_t)bp->size)
{
- bp->size = ((current_size + needed) & (size_t)~1023);
+ bp->size = ROUND_UP (current_size + needed, 1024);
bp->ptr = xrealloc(bp->ptr, bp->size);
if (bp->ptr == NULL)
{
--- a/src/indent.h
+++ b/src/indent.h
@@ -66,6 +66,14 @@
#include "lexi.h"
+/**
+ * Round up P to be a multiple of SIZE.
+ */
+
+#ifndef ROUND_UP
+#define ROUND_UP(p, size) (((unsigned long) (p) + (size) - 1) & ~((size) - 1))
+#endif
+
/** Values that `indent' can return for exit status.
*
* `total_success' means no errors or warnings were found during a successful
--- a/src/parse.c
+++ b/src/parse.c
@@ -53,7 +53,7 @@
parser_state_ty *parser_state_tos = NULL;
-#define INITIAL_BUFFER_SIZE 1000
+#define INITIAL_BUFFER_SIZE 1024
#define INITIAL_STACK_SIZE 2
/**
|