From: Bruno Haible Date: Wed, 23 Apr 2025 14:50:34 +0000 (+0200) Subject: Refactor: Make format-lua.c more similar to format-elisp.c. X-Git-Tag: v0.25~58 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ad68d1c6dd8289e68a6f0466baa0a3902a01a52f;p=thirdparty%2Fgettext.git Refactor: Make format-lua.c more similar to format-elisp.c. * gettext-tools/src/format-lua.c (format_parse): Allocate the final spec on the heap only once parsing was successful. --- diff --git a/gettext-tools/src/format-lua.c b/gettext-tools/src/format-lua.c index 1f0b8920f..d392216d9 100644 --- a/gettext-tools/src/format-lua.c +++ b/gettext-tools/src/format-lua.c @@ -72,8 +72,6 @@ struct spec enum format_arg_type *format_args; }; -static void format_free (void *descr); - static void * format_parse (const char *format, bool translated, char *fdi, char **invalid_reason) @@ -81,13 +79,13 @@ format_parse (const char *format, bool translated, char *fdi, const char *format_start = format; const char *fatstr = format; - struct spec *result = NULL; + struct spec spec; + struct spec *result; unsigned int format_args_allocated; - result = XMALLOC (struct spec); - result->directives = 0; - result->format_args_count = 0; - result->format_args = NULL; + spec.directives = 0; + spec.format_args_count = 0; + spec.format_args = NULL; format_args_allocated = 0; for (; *fatstr != '\0';) @@ -95,7 +93,7 @@ format_parse (const char *format, bool translated, char *fdi, if (*fatstr++ == '%') { FDI_SET (fatstr - 1, FMTDIR_START); - result->directives++; + spec.directives++; if (*fatstr != '%') { @@ -151,33 +149,35 @@ format_parse (const char *format, bool translated, char *fdi, else { *invalid_reason = - INVALID_CONVERSION_SPECIFIER (result-> - format_args_count + 1, + INVALID_CONVERSION_SPECIFIER (spec.format_args_count + 1, *fatstr); FDI_SET (fatstr, FMTDIR_ERROR); } - goto fmt_error; + goto bad_format; } - if (result->format_args_count == format_args_allocated) + if (spec.format_args_count == format_args_allocated) { format_args_allocated = 2 * format_args_allocated + 10; - result->format_args = - xrealloc (result->format_args, + spec.format_args = + xrealloc (spec.format_args, format_args_allocated * sizeof (enum format_arg_type)); } - result->format_args[result->format_args_count++] = type; + spec.format_args[spec.format_args_count++] = type; } FDI_SET (fatstr, FMTDIR_END); fatstr++; } } + result = XMALLOC (struct spec); + *result = spec; return result; -fmt_error: - format_free (result); + bad_format: + if (spec.format_args != NULL) + free (spec.format_args); return NULL; } @@ -255,6 +255,7 @@ struct formatstring_parser formatstring_lua = format_check }; + #ifdef TEST /* Test program: Print the argument list specification returned by