texinfo/XS-avoid-memory-leak.patch
2019-09-30 11:18:22 -04:00

148 lines
4.7 KiB
Diff

From 0cdd9c75042f86985617fff4e2a195ddb28a07c6 Mon Sep 17 00:00:00 2001
From: Gavin Smith <gavinsmith0123@gmail.com>
Date: Thu, 16 Aug 2018 16:11:53 +0000
Subject: [PATCH 144/759] XS avoid memory leak
git-svn-id: svn://127.0.0.1/svn_repo/texinfo/trunk@8096 39fee189-59d7-47db-b5d4-205258b72aed
---
ChangeLog | 7 +++++++
tp/Texinfo/XS/text.c | 13 ++++++++++++-
tp/Texinfo/XS/text.h | 3 ++-
tp/Texinfo/XS/xspara.c | 26 +++++++++++++-------------
4 files changed, 34 insertions(+), 15 deletions(-)
diff --git a/tp/Texinfo/Convert/XSParagraph/text.c b/tp/Texinfo/Convert/XSParagraph/text.c
index d686373b2..6baf8da3d 100644
--- a/tp/Texinfo/Convert/XSParagraph/text.c
+++ b/tp/Texinfo/Convert/XSParagraph/text.c
@@ -1,4 +1,4 @@
-/* Copyright 2014, 2015, 2016 Free Software Foundation, Inc.
+/* Copyright 2014, 2015, 2016, 2018 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -68,6 +68,17 @@ text_append (TEXT *t, char *s)
text_append_n (t, s, len);
}
+/* Set text to an empty string without clearing any storage */
+void
+text_reset (TEXT *t)
+{
+ if (t->end > 0)
+ {
+ t->end = 0;
+ t->text[0] = 0;
+ }
+}
+
void
text_init (TEXT *t)
{
diff --git a/tp/Texinfo/Convert/XSParagraph/text.h b/tp/Texinfo/Convert/XSParagraph/text.h
index e8fa9f915..7f14b3d29 100644
--- a/tp/Texinfo/Convert/XSParagraph/text.h
+++ b/tp/Texinfo/Convert/XSParagraph/text.h
@@ -1,4 +1,4 @@
-/* Copyright 2014, 2015, 2016 Free Software Foundation, Inc.
+/* Copyright 2014, 2015, 2016, 2018 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -20,6 +20,7 @@ typedef struct TEXT {
} TEXT;
void text_init (TEXT *t);
+void text_reset (TEXT *t);
void text_append (TEXT *t, char *s);
void text_append_n (TEXT *t, char *s, size_t len);
void text_printf (TEXT *t, char *format, ...);
diff --git a/tp/Texinfo/Convert/XSParagraph/xspara.c b/tp/Texinfo/Convert/XSParagraph/xspara.c
index f2d6d1ccd..52a54ed74 100644
--- a/tp/Texinfo/Convert/XSParagraph/xspara.c
+++ b/tp/Texinfo/Convert/XSParagraph/xspara.c
@@ -1,4 +1,4 @@
-/* Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free Software
+/* Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2018 Free Software
Foundation, Inc.
This program is free software: you can redistribute it and/or modify
@@ -553,8 +553,8 @@ xspara_end_line (void)
char *
xspara_get_pending (void)
{
- TEXT t;
- text_init (&t);
+ static TEXT t;
+ text_reset (&t);
text_append_n (&t, state.space.text, state.space.end);
text_append_n (&t, state.word.text, state.word.end);
return t.text;
@@ -610,9 +610,9 @@ xspara__add_pending_word (TEXT *result, int add_spaces)
char *
xspara_add_pending_word (int add_spaces)
{
- TEXT ret;
+ static TEXT ret;
- text_init (&ret);
+ text_reset (&ret);
state.end_line_count = 0;
xspara__add_pending_word (&ret, add_spaces);
if (ret.text)
@@ -625,8 +625,8 @@ xspara_add_pending_word (int add_spaces)
char *
xspara_end (void)
{
- TEXT ret;
- text_init (&ret);
+ static TEXT ret;
+ text_reset (&ret);
state.end_line_count = 0;
xspara__add_pending_word (&ret, state.add_final_space);
if (!state.no_final_newline && state.counter != 0)
@@ -773,9 +773,9 @@ xspara__add_next (TEXT *result, char *word, int word_len, int transparent)
char *
xspara_add_next (char *text, int text_len, int transparent)
{
- TEXT t;
+ static TEXT t;
- text_init (&t);
+ text_reset (&t);
state.end_line_count = 0;
xspara__add_next (&t, text, text_len, transparent);
@@ -883,10 +883,10 @@ xspara_add_text (char *text)
int len;
wchar_t wc;
size_t char_len;
- TEXT result;
+ static TEXT result;
dTHX;
- text_init (&result);
+ text_reset (&result);
len = strlen (text); /* FIXME: Get this as an argument */
state.end_line_count = 0;
@@ -973,13 +973,13 @@ xspara_add_text (char *text)
/* Truncate to at most 2 spaces, and replace any
'\n' or '\r' characters with ' '. */
- TEXT new_space;
+ static TEXT new_space;
char *pspace;
int pspace_left;
int len;
int i;
- text_init (&new_space);
+ text_reset (&new_space);
pspace = state.space.text;
pspace_left = state.space.end;
state.space_counter = 0;
--
2.19.1