bug-texinfo
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: free used incorrectly in api.c


From: Gavin Smith
Subject: Re: free used incorrectly in api.c
Date: Sun, 23 Oct 2022 13:55:43 +0100

On Sun, Oct 23, 2022 at 03:05:56PM +0300, Eli Zaretskii wrote:
> OK, I can try a patch to that effect if/when you have it.
> 
> Thanks.

It's quite a long patch:

diff --git a/ChangeLog b/ChangeLog
index 2db2d8ad94..c72c83ddeb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,26 @@
+2022-10-23  Gavin Smith  <gavinsmith0123@gmail.com>
+
+       Avoid calling Perl version of 'free'
+
+       * tp/Texinfo/XS/parsetexi/api.c (reset_parser):
+       Do not call 'free' - do everything via functions in other files.
+
+       * tp/Texinfo/XS/parsetexi/api.c
+       (set_input_file_name_encoding, conf_set_input_file_name_encoding)
+       (set_locale_encoding, conf_set_locale_encoding)
+       (set_documentlanguage_override, conf_set_documentlanguage_override):
+       Provide wrappers with names starting with 'conf_', and move the
+       implementations to other files.
+
+       * tp/Texinfo/XS/parsetexi/input.c
+       (set_input_file_name_encoding, set_locale_encoding): Add.  Allow
+       null arguments.
+       * tp/Texinfo/XS/parsetexi/parser.c
+       (set_documentlanguage_override): Add.  Allow null argument.
+       (set_documentlanguage):  Allow null argument.
+
+       Report from Eli, who got "Free to wrong pool" error.
+
 2022-10-23  Gavin Smith  <gavinsmith0123@gmail.com>
 
        * install-info/tests/README: Complete list of tests (not
diff --git a/tp/Texinfo/XS/parsetexi/Parsetexi.pm 
b/tp/Texinfo/XS/parsetexi/Parsetexi.pm
index 3c87a0b202..25f3672ae1 100644
--- a/tp/Texinfo/XS/parsetexi/Parsetexi.pm
+++ b/tp/Texinfo/XS/parsetexi/Parsetexi.pm
@@ -125,7 +125,7 @@ sub parser (;$$)
         }
       } elsif ($key eq 'documentlanguage') {
         if (defined ($conf->{$key})) {
-          set_documentlanguage_override ($conf->{$key});
+          conf_set_documentlanguage_override ($conf->{$key});
         }
       } elsif ($key eq 'FORMAT_MENU') {
         if ($conf->{$key} eq 'menu') {
@@ -141,14 +141,10 @@ sub parser (;$$)
         set_debug($conf->{$key}) if $conf->{$key};
       } elsif ($key eq 'DOC_ENCODING_FOR_INPUT_FILE_NAME') {
         set_DOC_ENCODING_FOR_INPUT_FILE_NAME ($conf->{$key});
-      } elsif ($key eq 'INPUT_FILE_NAME_ENCODING') {
-        if (defined ($conf->{$key})) {
-          set_input_file_name_encoding ($conf->{$key});
-        }
-      } elsif ($key eq 'LOCALE_ENCODING') {
-        if (defined ($conf->{$key})) {
-          set_locale_encoding ($conf->{$key});
-        }
+      } elsif ($key eq 'INPUT_FILE_NAME_ENCODING' and defined($conf->{$key})) {
+        conf_set_input_file_name_encoding ($conf->{$key});
+      } elsif ($key eq 'LOCALE_ENCODING' and defined($conf->{$key})) {
+        conf_set_locale_encoding ($conf->{$key});
       } elsif ($key eq 'accept_internalvalue') {
         set_accept_internalvalue();
       } elsif ($key eq 'registrar' or $key eq 'COMMAND_LINE_ENCODING') {
diff --git a/tp/Texinfo/XS/parsetexi/Parsetexi.xs 
b/tp/Texinfo/XS/parsetexi/Parsetexi.xs
index 29bae99686..d5972061fa 100644
--- a/tp/Texinfo/XS/parsetexi/Parsetexi.xs
+++ b/tp/Texinfo/XS/parsetexi/Parsetexi.xs
@@ -110,15 +110,15 @@ void
 set_DOC_ENCODING_FOR_INPUT_FILE_NAME (int i)
 
 void
-set_input_file_name_encoding (value)
+conf_set_input_file_name_encoding (value)
      char *value
 
 void
-set_locale_encoding (value)
+conf_set_locale_encoding (value)
      char *value
 
 void
-set_documentlanguage_override (value)
+conf_set_documentlanguage_override (value)
      char *value
 
 void
diff --git a/tp/Texinfo/XS/parsetexi/api.c b/tp/Texinfo/XS/parsetexi/api.c
index 0178732d72..8066dcad0c 100644
--- a/tp/Texinfo/XS/parsetexi/api.c
+++ b/tp/Texinfo/XS/parsetexi/api.c
@@ -142,7 +142,10 @@ reset_parser_except_conf (void)
 void
 reset_parser (void)
 {
-  dTHX;
+  /* NOTE: Do not call 'malloc' or 'free' in this function or in any function
+     called in this file.  Since this file (api.c) includes the Perl headers,
+     we get the Perl redefinitions, which we do not want, as we don't use
+     them throughout the rest of the program. */
 
   debug ("!!!!!!!!!!!!!!!! RESETTING THE PARSER !!!!!!!!!!!!!!!!!!!!!");
 
@@ -152,15 +155,12 @@ reset_parser (void)
   clear_include_directories ();
   reset_conf ();
 
-  free (global_documentlanguage);
-  global_documentlanguage = 0;
   global_documentlanguage_fixed = 0;
+  set_documentlanguage (0);
 
   doc_encoding_for_input_file_name = 1;
-  free (input_file_name_encoding);
-  input_file_name_encoding = 0;
-  free (locale_encoding);
-  locale_encoding = 0;
+  set_input_file_name_encoding (0);
+  set_locale_encoding (0);
 
   global_accept_internalvalue = 0;
 }
@@ -1058,13 +1058,9 @@ set_debug (int value)
 }
 
 void
-set_documentlanguage_override (char *value)
+conf_set_documentlanguage_override (char *value)
 {
-  dTHX;
-
-  free (global_documentlanguage);
-  global_documentlanguage = strdup (value);
-  global_documentlanguage_fixed = 1;
+  set_documentlanguage_override (value);
 }
 
 
@@ -1075,21 +1071,15 @@ set_DOC_ENCODING_FOR_INPUT_FILE_NAME (int i)
 }
 
 void
-set_input_file_name_encoding (char *value)
+conf_set_input_file_name_encoding (char *value)
 {
-  dTHX;
-
-  free (input_file_name_encoding);
-  input_file_name_encoding = strdup (value);
+  set_input_file_name_encoding (value);
 }
 
 void
-set_locale_encoding (char *value)
+conf_set_locale_encoding (char *value)
 {
-  dTHX;
-
-  free (locale_encoding);
-  locale_encoding = strdup (value);
+  set_locale_encoding (value);
 }
 
 
diff --git a/tp/Texinfo/XS/parsetexi/api.h b/tp/Texinfo/XS/parsetexi/api.h
index 0badce2467..626c3b4c7d 100644
--- a/tp/Texinfo/XS/parsetexi/api.h
+++ b/tp/Texinfo/XS/parsetexi/api.h
@@ -14,10 +14,11 @@ void reset_parser_except_conf (void);
 void set_debug (int);
 void wipe_values (void);
 void reset_context_stack (void);
-void set_documentlanguage_override (char *value);
+
 void set_DOC_ENCODING_FOR_INPUT_FILE_NAME (int i);
-void set_input_file_name_encoding (char *value);
-void set_locale_encoding (char *value);
+void conf_set_input_file_name_encoding (char *value);
+void conf_set_locale_encoding (char *value);
+void conf_set_documentlanguage_override (char *value);
 
 HV *build_texinfo_tree (void);
 AV *build_label_list (void);
diff --git a/tp/Texinfo/XS/parsetexi/input.c b/tp/Texinfo/XS/parsetexi/input.c
index a3626bdc10..c8730b4d6a 100644
--- a/tp/Texinfo/XS/parsetexi/input.c
+++ b/tp/Texinfo/XS/parsetexi/input.c
@@ -284,6 +284,20 @@ int doc_encoding_for_input_file_name = 1;
 char *input_file_name_encoding = 0;
 char *locale_encoding = 0;
 
+void
+set_input_file_name_encoding (char *value)
+{
+  free (input_file_name_encoding);
+  input_file_name_encoding = value ? strdup (value) : 0;
+}
+
+void
+set_locale_encoding (char *value)
+{
+  free (locale_encoding);
+  locale_encoding =  value ? strdup (value) : 0;
+}
+
 /* Reverse the decoding of the filename to the input encoding, to retrieve
    the bytes that were present in the original Texinfo file.  Return
    value is freed by free_small_strings. */
diff --git a/tp/Texinfo/XS/parsetexi/input.h b/tp/Texinfo/XS/parsetexi/input.h
index 4a50814bef..9cf758e030 100644
--- a/tp/Texinfo/XS/parsetexi/input.h
+++ b/tp/Texinfo/XS/parsetexi/input.h
@@ -35,4 +35,7 @@ extern int doc_encoding_for_input_file_name;
 extern char *input_file_name_encoding;
 extern char *locale_encoding;
 
+void set_input_file_name_encoding (char *value);
+void set_locale_encoding (char *value);
+
 #endif
diff --git a/tp/Texinfo/XS/parsetexi/parser.c b/tp/Texinfo/XS/parsetexi/parser.c
index 1e3f19bea1..8a85a14c2e 100644
--- a/tp/Texinfo/XS/parsetexi/parser.c
+++ b/tp/Texinfo/XS/parsetexi/parser.c
@@ -169,10 +169,19 @@ set_documentlanguage (char *value)
   if (!global_documentlanguage_fixed)
     {
       free (global_documentlanguage);
-      global_documentlanguage = strdup (value);
+      global_documentlanguage = value ? strdup (value) : 0;
     }
 }
 
+void
+set_documentlanguage_override (char *value)
+{
+  free (global_documentlanguage);
+  global_documentlanguage = value ? strdup (value) : 0;
+  global_documentlanguage_fixed = 1;
+}
+
+
 void
 set_accept_internalvalue()
 {
diff --git a/tp/Texinfo/XS/parsetexi/parser.h b/tp/Texinfo/XS/parsetexi/parser.h
index d01d495a05..7303a53357 100644
--- a/tp/Texinfo/XS/parsetexi/parser.h
+++ b/tp/Texinfo/XS/parsetexi/parser.h
@@ -169,6 +169,7 @@ int format_expanded_p (char *format);
 int is_end_current_command (ELEMENT *current, char **line,
                             enum command_id *end_cmd);
 void set_documentlanguage (char *);
+void set_documentlanguage_override (char *value);
 void set_accept_internalvalue (void);
 char *element_type_name (ELEMENT *e);
 int check_space_element (ELEMENT *e);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]