fe_utils: Sprinkle some pg_malloc_object() and pg_malloc_array()

The idea is to encourage more the use of these allocation routines
across the tree, as these offer stronger type safety guarantees than
pg_malloc() & friends (type cast in the result, sizeof() embedded).
This commit updates some code paths of src/fe_utils/.

This commit is similar to 31d3847a37.

Author: Henrik TJ <henrik@0x48.dk>
Reviewed-by: Andreas Karlsson <andreas@proxel.se>
Discussion: https://postgr.es/m/6df1b64e-1314-9afd-41a3-3fefb76225e1@0x48.dk
This commit is contained in:
Michael Paquier 2026-02-24 12:34:42 +09:00
parent bfc321b472
commit ff393fa526
4 changed files with 32 additions and 32 deletions

View file

@ -17,7 +17,7 @@
ConditionalStack
conditional_stack_create(void)
{
ConditionalStack cstack = pg_malloc(sizeof(ConditionalStackData));
ConditionalStack cstack = pg_malloc_object(ConditionalStackData);
cstack->head = NULL;
return cstack;
@ -52,7 +52,7 @@ conditional_stack_destroy(ConditionalStack cstack)
void
conditional_stack_push(ConditionalStack cstack, ifState new_state)
{
IfStackElem *p = (IfStackElem *) pg_malloc(sizeof(IfStackElem));
IfStackElem *p = pg_malloc_object(IfStackElem);
p->if_state = new_state;
p->query_len = -1;

View file

@ -344,7 +344,7 @@ format_numeric_locale(const char *my_str)
return pg_strdup(my_str);
new_len = strlen(my_str) + additional_numeric_locale_len(my_str);
new_str = pg_malloc(new_len + 1);
new_str = pg_malloc_array(char, (new_len + 1));
new_str_pos = 0;
int_len = integer_digits(my_str);
@ -692,18 +692,18 @@ print_aligned_text(const printTableContent *cont, FILE *fout, bool is_pager)
if (cont->ncolumns > 0)
{
col_count = cont->ncolumns;
width_header = pg_malloc0(col_count * sizeof(*width_header));
width_average = pg_malloc0(col_count * sizeof(*width_average));
max_width = pg_malloc0(col_count * sizeof(*max_width));
width_wrap = pg_malloc0(col_count * sizeof(*width_wrap));
max_nl_lines = pg_malloc0(col_count * sizeof(*max_nl_lines));
curr_nl_line = pg_malloc0(col_count * sizeof(*curr_nl_line));
col_lineptrs = pg_malloc0(col_count * sizeof(*col_lineptrs));
max_bytes = pg_malloc0(col_count * sizeof(*max_bytes));
format_buf = pg_malloc0(col_count * sizeof(*format_buf));
header_done = pg_malloc0(col_count * sizeof(*header_done));
bytes_output = pg_malloc0(col_count * sizeof(*bytes_output));
wrap = pg_malloc0(col_count * sizeof(*wrap));
width_header = pg_malloc0_array(unsigned int, col_count);
width_average = pg_malloc0_array(unsigned int, col_count);
max_width = pg_malloc0_array(unsigned int, col_count);
width_wrap = pg_malloc0_array(unsigned int, col_count);
max_nl_lines = pg_malloc0_array(unsigned int, col_count);
curr_nl_line = pg_malloc0_array(unsigned int, col_count);
col_lineptrs = pg_malloc0_array(struct lineptr *, col_count);
max_bytes = pg_malloc0_array(unsigned int, col_count);
format_buf = pg_malloc0_array(unsigned char *, col_count);
header_done = pg_malloc0_array(bool, col_count);
bytes_output = pg_malloc0_array(int, col_count);
wrap = pg_malloc0_array(printTextLineWrap, col_count);
}
else
{
@ -798,10 +798,10 @@ print_aligned_text(const printTableContent *cont, FILE *fout, bool is_pager)
for (i = 0; i < col_count; i++)
{
/* Add entry for ptr == NULL array termination */
col_lineptrs[i] = pg_malloc0((max_nl_lines[i] + 1) *
sizeof(**col_lineptrs));
col_lineptrs[i] = pg_malloc0_array(struct lineptr,
(max_nl_lines[i] + 1));
format_buf[i] = pg_malloc(max_bytes[i] + 1);
format_buf[i] = pg_malloc_array(unsigned char, (max_bytes[i] + 1));
col_lineptrs[i]->ptr = format_buf[i];
}
@ -1409,8 +1409,8 @@ print_aligned_vertical(const printTableContent *cont,
* We now have all the information we need to setup the formatting
* structures
*/
dlineptr = pg_malloc((sizeof(*dlineptr)) * (dheight + 1));
hlineptr = pg_malloc((sizeof(*hlineptr)) * (hheight + 1));
dlineptr = pg_malloc_array(struct lineptr, (dheight + 1));
hlineptr = pg_malloc_array(struct lineptr, (hheight + 1));
dlineptr->ptr = pg_malloc(dformatsize);
hlineptr->ptr = pg_malloc(hformatsize);
@ -3198,7 +3198,7 @@ printTableInit(printTableContent *const content, const printTableOpt *opt,
content->ncolumns = ncolumns;
content->nrows = nrows;
content->headers = pg_malloc0((ncolumns + 1) * sizeof(*content->headers));
content->headers = pg_malloc0_array(const char *, (ncolumns + 1));
total_cells = (uint64) ncolumns * nrows;
/* Catch possible overflow. Using >= here allows adding 1 below */
@ -3209,12 +3209,12 @@ printTableInit(printTableContent *const content, const printTableOpt *opt,
SIZE_MAX / sizeof(*content->cells));
exit(EXIT_FAILURE);
}
content->cells = pg_malloc0((total_cells + 1) * sizeof(*content->cells));
content->cells = pg_malloc0_array(const char *, (total_cells + 1));
content->cellmustfree = NULL;
content->footers = NULL;
content->aligns = pg_malloc0((ncolumns + 1) * sizeof(*content->align));
content->aligns = pg_malloc0_array(char, (ncolumns + 1));
content->header = content->headers;
content->cell = content->cells;
@ -3305,7 +3305,7 @@ printTableAddCell(printTableContent *const content, char *cell,
{
if (content->cellmustfree == NULL)
content->cellmustfree =
pg_malloc0((total_cells + 1) * sizeof(bool));
pg_malloc0_array(bool, (total_cells + 1));
content->cellmustfree[content->cellsadded] = true;
}
@ -3330,7 +3330,7 @@ printTableAddFooter(printTableContent *const content, const char *footer)
{
printTableFooter *f;
f = pg_malloc0(sizeof(*f));
f = pg_malloc0_object(printTableFooter);
f->data = pg_strdup(footer);
if (content->footers == NULL)
@ -3477,7 +3477,7 @@ count_table_lines(const printTableContent *cont,
* Scan all column headers and determine their heights. Cache the values
* since vertical mode repeats the headers for every record.
*/
header_height = (int *) pg_malloc(cont->ncolumns * sizeof(int));
header_height = pg_malloc_array(int, cont->ncolumns);
for (i = 0; i < cont->ncolumns; i++)
{
pg_wcssize((const unsigned char *) cont->headers[i],

View file

@ -1002,7 +1002,7 @@ psql_scan_create(const PsqlScanCallbacks *callbacks)
{
PsqlScanState state;
state = (PsqlScanStateData *) pg_malloc0(sizeof(PsqlScanStateData));
state = pg_malloc0_object(PsqlScanStateData);
state->callbacks = callbacks;
@ -1376,7 +1376,7 @@ psqlscan_push_new_buffer(PsqlScanState state, const char *newstr,
{
StackElem *stackelem;
stackelem = (StackElem *) pg_malloc(sizeof(StackElem));
stackelem = pg_malloc_object(StackElem);
/*
* In current usage, the passed varname points at the current flex input
@ -1479,7 +1479,7 @@ psqlscan_prepare_buffer(PsqlScanState state, const char *txt, int len,
char *newtxt;
/* Flex wants two \0 characters after the actual data */
newtxt = pg_malloc(len + 2);
newtxt = pg_malloc_array(char, (len + 2));
*txtcopy = newtxt;
newtxt[len] = newtxt[len + 1] = YY_END_OF_BUFFER_CHAR;
@ -1548,7 +1548,7 @@ psqlscan_emit(PsqlScanState state, const char *txt, int len)
char *
psqlscan_extract_substring(PsqlScanState state, const char *txt, int len)
{
char *result = (char *) pg_malloc(len + 1);
char *result = pg_malloc_array(char, (len + 1));
if (state->safe_encoding)
memcpy(result, txt, len);

View file

@ -27,7 +27,7 @@ simple_oid_list_append(SimpleOidList *list, Oid val)
{
SimpleOidListCell *cell;
cell = (SimpleOidListCell *) pg_malloc(sizeof(SimpleOidListCell));
cell = pg_malloc_object(SimpleOidListCell);
cell->next = NULL;
cell->val = val;
@ -163,7 +163,7 @@ simple_ptr_list_append(SimplePtrList *list, void *ptr)
{
SimplePtrListCell *cell;
cell = (SimplePtrListCell *) pg_malloc(sizeof(SimplePtrListCell));
cell = pg_malloc_object(SimplePtrListCell);
cell->next = NULL;
cell->ptr = ptr;