mirror of
https://sourceware.org/git/glibc.git
synced 2025-08-07 06:43:00 +03:00
gencat: Get rid of alloca.
Convert to scratch_buffers to avoid potential stack overflow. Checked on x86_64-linux-gnu and aarch64-linux-gnu. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
This commit is contained in:
@@ -32,6 +32,7 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <nl_types.h>
|
#include <nl_types.h>
|
||||||
#include <obstack.h>
|
#include <obstack.h>
|
||||||
|
#include <scratch_buffer.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -854,6 +855,10 @@ write_out (struct catalog *catalog, const char *output_name,
|
|||||||
uint32_t *array1, *array2;
|
uint32_t *array1, *array2;
|
||||||
size_t cnt;
|
size_t cnt;
|
||||||
int fd;
|
int fd;
|
||||||
|
struct scratch_buffer buf1;
|
||||||
|
scratch_buffer_init (&buf1);
|
||||||
|
struct scratch_buffer buf2;
|
||||||
|
scratch_buffer_init (&buf2);
|
||||||
|
|
||||||
/* If not otherwise told try to read file with existing
|
/* If not otherwise told try to read file with existing
|
||||||
translations. */
|
translations. */
|
||||||
@@ -929,9 +934,19 @@ write_out (struct catalog *catalog, const char *output_name,
|
|||||||
|
|
||||||
uint32_t array_size = best_size * best_depth * sizeof (uint32_t) * 3;
|
uint32_t array_size = best_size * best_depth * sizeof (uint32_t) * 3;
|
||||||
/* Allocate room for all needed arrays. */
|
/* Allocate room for all needed arrays. */
|
||||||
array1 = (uint32_t *) alloca (array_size);
|
if (!scratch_buffer_set_array_size (&buf1, best_size * best_depth * 3,
|
||||||
|
sizeof (uint32_t)))
|
||||||
|
error (EXIT_FAILURE, ENOMEM, gettext ("cannot allocate memory"));
|
||||||
|
array1 = buf1.data;
|
||||||
memset (array1, '\0', array_size);
|
memset (array1, '\0', array_size);
|
||||||
array2 = (uint32_t *) alloca (array_size);
|
|
||||||
|
if (!scratch_buffer_set_array_size (&buf2, best_size * best_depth * 3,
|
||||||
|
sizeof (uint32_t)))
|
||||||
|
{
|
||||||
|
scratch_buffer_free (&buf1);
|
||||||
|
error (EXIT_FAILURE, ENOMEM, gettext ("cannot allocate memory"));
|
||||||
|
}
|
||||||
|
array2 = buf2.data;
|
||||||
obstack_init (&string_pool);
|
obstack_init (&string_pool);
|
||||||
|
|
||||||
set_run = catalog->all_sets;
|
set_run = catalog->all_sets;
|
||||||
@@ -979,9 +994,13 @@ write_out (struct catalog *catalog, const char *output_name,
|
|||||||
{
|
{
|
||||||
fd = creat (output_name, 0666);
|
fd = creat (output_name, 0666);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
|
{
|
||||||
|
scratch_buffer_free (&buf1);
|
||||||
|
scratch_buffer_free (&buf2);
|
||||||
error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"),
|
error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"),
|
||||||
output_name);
|
output_name);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Write out header. */
|
/* Write out header. */
|
||||||
write_all(fd, &obj, sizeof (obj));
|
write_all(fd, &obj, sizeof (obj));
|
||||||
@@ -1019,9 +1038,13 @@ write_out (struct catalog *catalog, const char *output_name,
|
|||||||
{
|
{
|
||||||
fp = fopen (header_name, "w");
|
fp = fopen (header_name, "w");
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
|
{
|
||||||
|
scratch_buffer_free (&buf1);
|
||||||
|
scratch_buffer_free (&buf2);
|
||||||
error (EXIT_FAILURE, errno,
|
error (EXIT_FAILURE, errno,
|
||||||
gettext ("cannot open output file `%s'"), header_name);
|
gettext ("cannot open output file `%s'"), header_name);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Iterate over all sets and all messages. */
|
/* Iterate over all sets and all messages. */
|
||||||
set_run = catalog->all_sets;
|
set_run = catalog->all_sets;
|
||||||
@@ -1066,6 +1089,8 @@ write_out (struct catalog *catalog, const char *output_name,
|
|||||||
if (fp != stdout)
|
if (fp != stdout)
|
||||||
fclose (fp);
|
fclose (fp);
|
||||||
}
|
}
|
||||||
|
scratch_buffer_free (&buf1);
|
||||||
|
scratch_buffer_free (&buf2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user