1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-04 20:11:56 +03:00

formatting.c cleanup: Use size_t for string length variables and arguments

Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/6dd9d208-a3ed-49b5-b03d-8617261da973%40eisentraut.org
This commit is contained in:
Peter Eisentraut
2025-10-29 09:27:59 +01:00
parent f0f2c0c1ae
commit d98b3cdbaf

View File

@@ -118,9 +118,9 @@
typedef struct typedef struct
{ {
const char *name; /* suffix string */ const char *name; /* suffix string */
int len, /* suffix length */ size_t len; /* suffix length */
id, /* used in node->suffix */ int id; /* used in node->suffix */
type; /* prefix / postfix */ int type; /* prefix / postfix */
} KeySuffix; } KeySuffix;
/* /*
@@ -139,7 +139,7 @@ typedef enum
typedef struct typedef struct
{ {
const char *name; const char *name;
int len; size_t len;
int id; int id;
bool is_digit; bool is_digit;
FromCharDateMode date_mode; FromCharDateMode date_mode;
@@ -1058,17 +1058,17 @@ static void dump_node(FormatNode *node, int max);
static const char *get_th(char *num, int type); static const char *get_th(char *num, int type);
static char *str_numth(char *dest, char *num, int type); static char *str_numth(char *dest, char *num, int type);
static int adjust_partial_year_to_2020(int year); static int adjust_partial_year_to_2020(int year);
static int strspace_len(const char *str); static size_t strspace_len(const char *str);
static bool from_char_set_mode(TmFromChar *tmfc, const FromCharDateMode mode, static bool from_char_set_mode(TmFromChar *tmfc, const FromCharDateMode mode,
Node *escontext); Node *escontext);
static bool from_char_set_int(int *dest, const int value, const FormatNode *node, static bool from_char_set_int(int *dest, const int value, const FormatNode *node,
Node *escontext); Node *escontext);
static int from_char_parse_int_len(int *dest, const char **src, const int len, static int from_char_parse_int_len(int *dest, const char **src, const size_t len,
FormatNode *node, Node *escontext); FormatNode *node, Node *escontext);
static int from_char_parse_int(int *dest, const char **src, FormatNode *node, static int from_char_parse_int(int *dest, const char **src, FormatNode *node,
Node *escontext); Node *escontext);
static int seq_search_ascii(const char *name, const char *const *array, int *len); static int seq_search_ascii(const char *name, const char *const *array, size_t *len);
static int seq_search_localized(const char *name, char **array, int *len, static int seq_search_localized(const char *name, char **array, size_t *len,
Oid collid); Oid collid);
static bool from_char_seq_search(int *dest, const char **src, static bool from_char_seq_search(int *dest, const char **src,
const char *const *array, const char *const *array,
@@ -1080,13 +1080,13 @@ static bool do_to_timestamp(text *date_txt, text *fmt, Oid collid, bool std,
static char *fill_str(char *str, int c, int max); static char *fill_str(char *str, int c, int max);
static FormatNode *NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree); static FormatNode *NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree);
static char *int_to_roman(int number); static char *int_to_roman(int number);
static int roman_to_int(NUMProc *Np, int input_len); static int roman_to_int(NUMProc *Np, size_t input_len);
static void NUM_prepare_locale(NUMProc *Np); static void NUM_prepare_locale(NUMProc *Np);
static char *get_last_relevant_decnum(char *num); static char *get_last_relevant_decnum(char *num);
static void NUM_numpart_from_char(NUMProc *Np, int id, int input_len); static void NUM_numpart_from_char(NUMProc *Np, int id, size_t input_len);
static void NUM_numpart_to_char(NUMProc *Np, int id); static void NUM_numpart_to_char(NUMProc *Np, int id);
static char *NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, static char *NUM_processor(FormatNode *node, NUMDesc *Num, char *inout,
char *number, int input_len, int to_char_out_pre_spaces, char *number, size_t input_len, int to_char_out_pre_spaces,
int sign, bool is_to_char, Oid collid); int sign, bool is_to_char, Oid collid);
static DCHCacheEntry *DCH_cache_getnew(const char *str, bool std); static DCHCacheEntry *DCH_cache_getnew(const char *str, bool std);
static DCHCacheEntry *DCH_cache_search(const char *str, bool std); static DCHCacheEntry *DCH_cache_search(const char *str, bool std);
@@ -1526,8 +1526,8 @@ dump_node(FormatNode *node, int max)
static const char * static const char *
get_th(char *num, int type) get_th(char *num, int type)
{ {
int len = strlen(num), size_t len = strlen(num);
last; char last;
Assert(len > 0); Assert(len > 0);
@@ -2072,10 +2072,10 @@ adjust_partial_year_to_2020(int year)
} }
static int static size_t
strspace_len(const char *str) strspace_len(const char *str)
{ {
int len = 0; size_t len = 0;
while (*str && isspace((unsigned char) *str)) while (*str && isspace((unsigned char) *str))
{ {
@@ -2158,13 +2158,13 @@ from_char_set_int(int *dest, const int value, const FormatNode *node,
* with DD and MI). * with DD and MI).
*/ */
static int static int
from_char_parse_int_len(int *dest, const char **src, const int len, FormatNode *node, from_char_parse_int_len(int *dest, const char **src, const size_t len, FormatNode *node,
Node *escontext) Node *escontext)
{ {
long result; long result;
char copy[DCH_MAX_ITEM_SIZ + 1]; char copy[DCH_MAX_ITEM_SIZ + 1];
const char *init = *src; const char *init = *src;
int used; size_t used;
/* /*
* Skip any whitespace before parsing the integer. * Skip any whitespace before parsing the integer.
@@ -2172,7 +2172,7 @@ from_char_parse_int_len(int *dest, const char **src, const int len, FormatNode *
*src += strspace_len(*src); *src += strspace_len(*src);
Assert(len <= DCH_MAX_ITEM_SIZ); Assert(len <= DCH_MAX_ITEM_SIZ);
used = (int) strlcpy(copy, *src, len + 1); used = strlcpy(copy, *src, len + 1);
if (S_FM(node->suffix) || is_next_separator(node)) if (S_FM(node->suffix) || is_next_separator(node))
{ {
@@ -2199,7 +2199,7 @@ from_char_parse_int_len(int *dest, const char **src, const int len, FormatNode *
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
errmsg("source string too short for \"%s\" formatting field", errmsg("source string too short for \"%s\" formatting field",
node->key->name), node->key->name),
errdetail("Field requires %d characters, but only %d remain.", errdetail("Field requires %zu characters, but only %zu remain.",
len, used), len, used),
errhint("If your source string is not fixed-width, " errhint("If your source string is not fixed-width, "
"try using the \"FM\" modifier."))); "try using the \"FM\" modifier.")));
@@ -2213,7 +2213,7 @@ from_char_parse_int_len(int *dest, const char **src, const int len, FormatNode *
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
errmsg("invalid value \"%s\" for \"%s\"", errmsg("invalid value \"%s\" for \"%s\"",
copy, node->key->name), copy, node->key->name),
errdetail("Field requires %d characters, but only %d could be parsed.", errdetail("Field requires %zu characters, but only %zu could be parsed.",
len, used), len, used),
errhint("If your source string is not fixed-width, " errhint("If your source string is not fixed-width, "
"try using the \"FM\" modifier."))); "try using the \"FM\" modifier.")));
@@ -2273,7 +2273,7 @@ from_char_parse_int(int *dest, const char **src, FormatNode *node,
* suitable for comparisons to ASCII strings. * suitable for comparisons to ASCII strings.
*/ */
static int static int
seq_search_ascii(const char *name, const char *const *array, int *len) seq_search_ascii(const char *name, const char *const *array, size_t *len)
{ {
unsigned char firstc; unsigned char firstc;
@@ -2326,7 +2326,7 @@ seq_search_ascii(const char *name, const char *const *array, int *len)
* the arrays exported by pg_locale.c aren't const. * the arrays exported by pg_locale.c aren't const.
*/ */
static int static int
seq_search_localized(const char *name, char **array, int *len, Oid collid) seq_search_localized(const char *name, char **array, size_t *len, Oid collid)
{ {
char *upper_name; char *upper_name;
char *lower_name; char *lower_name;
@@ -2343,7 +2343,7 @@ seq_search_localized(const char *name, char **array, int *len, Oid collid)
*/ */
for (char **a = array; *a != NULL; a++) for (char **a = array; *a != NULL; a++)
{ {
int element_len = strlen(*a); size_t element_len = strlen(*a);
if (strncmp(name, *a, element_len) == 0) if (strncmp(name, *a, element_len) == 0)
{ {
@@ -2364,7 +2364,7 @@ seq_search_localized(const char *name, char **array, int *len, Oid collid)
{ {
char *upper_element; char *upper_element;
char *lower_element; char *lower_element;
int element_len; size_t element_len;
/* Likewise upper/lower-case array element */ /* Likewise upper/lower-case array element */
upper_element = str_toupper(*a, strlen(*a), collid); upper_element = str_toupper(*a, strlen(*a), collid);
@@ -2413,7 +2413,7 @@ from_char_seq_search(int *dest, const char **src, const char *const *array,
char **localized_array, Oid collid, char **localized_array, Oid collid,
FormatNode *node, Node *escontext) FormatNode *node, Node *escontext)
{ {
int len; size_t len;
if (localized_array == NULL) if (localized_array == NULL)
*dest = seq_search_ascii(*src, array, &len); *dest = seq_search_ascii(*src, array, &len);
@@ -3881,7 +3881,7 @@ datetime_to_char_body(TmToChar *tmtc, text *fmt, bool is_interval, Oid collid)
char *fmt_str, char *fmt_str,
*result; *result;
bool incache; bool incache;
int fmt_len; size_t fmt_len;
text *res; text *res;
/* /*
@@ -4311,7 +4311,7 @@ bool
datetime_format_has_tz(const char *fmt_str) datetime_format_has_tz(const char *fmt_str)
{ {
bool incache; bool incache;
int fmt_len = strlen(fmt_str); size_t fmt_len = strlen(fmt_str);
int result; int result;
FormatNode *format; FormatNode *format;
@@ -5067,10 +5067,10 @@ int_to_roman(int number)
* If input is invalid, return -1. * If input is invalid, return -1.
*/ */
static int static int
roman_to_int(NUMProc *Np, int input_len) roman_to_int(NUMProc *Np, size_t input_len)
{ {
int result = 0; int result = 0;
int len; size_t len;
char romanChars[MAX_ROMAN_LEN]; char romanChars[MAX_ROMAN_LEN];
int romanValues[MAX_ROMAN_LEN]; int romanValues[MAX_ROMAN_LEN];
int repeatCount = 1; int repeatCount = 1;
@@ -5109,7 +5109,7 @@ roman_to_int(NUMProc *Np, int input_len)
return -1; /* No valid roman numerals. */ return -1; /* No valid roman numerals. */
/* Check for valid combinations and compute the represented value. */ /* Check for valid combinations and compute the represented value. */
for (int i = 0; i < len; i++) for (size_t i = 0; i < len; i++)
{ {
char currChar = romanChars[i]; char currChar = romanChars[i];
int currValue = romanValues[i]; int currValue = romanValues[i];
@@ -5324,7 +5324,7 @@ get_last_relevant_decnum(char *num)
* Number extraction for TO_NUMBER() * Number extraction for TO_NUMBER()
*/ */
static void static void
NUM_numpart_from_char(NUMProc *Np, int id, int input_len) NUM_numpart_from_char(NUMProc *Np, int id, size_t input_len)
{ {
bool isread = false; bool isread = false;
@@ -5358,7 +5358,7 @@ NUM_numpart_from_char(NUMProc *Np, int id, int input_len)
*/ */
if (IS_LSIGN(Np->Num) && Np->Num->lsign == NUM_LSIGN_PRE) if (IS_LSIGN(Np->Num) && Np->Num->lsign == NUM_LSIGN_PRE)
{ {
int x = 0; size_t x = 0;
#ifdef DEBUG_TO_FROM_CHAR #ifdef DEBUG_TO_FROM_CHAR
elog(DEBUG_elog_output, "Try read locale pre-sign (%c)", *Np->inout_p); elog(DEBUG_elog_output, "Try read locale pre-sign (%c)", *Np->inout_p);
@@ -5437,7 +5437,7 @@ NUM_numpart_from_char(NUMProc *Np, int id, int input_len)
* Np->decimal is always just "." if we don't have a D format token. * Np->decimal is always just "." if we don't have a D format token.
* So we just unconditionally match to Np->decimal. * So we just unconditionally match to Np->decimal.
*/ */
int x = strlen(Np->decimal); size_t x = strlen(Np->decimal);
#ifdef DEBUG_TO_FROM_CHAR #ifdef DEBUG_TO_FROM_CHAR
elog(DEBUG_elog_output, "Try read decimal point (%c)", elog(DEBUG_elog_output, "Try read decimal point (%c)",
@@ -5476,7 +5476,7 @@ NUM_numpart_from_char(NUMProc *Np, int id, int input_len)
(Np->inout_p + 1) < Np->inout + input_len && (Np->inout_p + 1) < Np->inout + input_len &&
!isdigit((unsigned char) *(Np->inout_p + 1))) !isdigit((unsigned char) *(Np->inout_p + 1)))
{ {
int x; size_t x;
char *tmp = Np->inout_p++; char *tmp = Np->inout_p++;
#ifdef DEBUG_TO_FROM_CHAR #ifdef DEBUG_TO_FROM_CHAR
@@ -5728,7 +5728,7 @@ NUM_numpart_to_char(NUMProc *Np, int id)
* Skip over "n" input characters, but only if they aren't numeric data * Skip over "n" input characters, but only if they aren't numeric data
*/ */
static void static void
NUM_eat_non_data_chars(NUMProc *Np, int n, int input_len) NUM_eat_non_data_chars(NUMProc *Np, int n, size_t input_len)
{ {
while (n-- > 0) while (n-- > 0)
{ {
@@ -5742,14 +5742,14 @@ NUM_eat_non_data_chars(NUMProc *Np, int n, int input_len)
static char * static char *
NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, NUM_processor(FormatNode *node, NUMDesc *Num, char *inout,
char *number, int input_len, int to_char_out_pre_spaces, char *number, size_t input_len, int to_char_out_pre_spaces,
int sign, bool is_to_char, Oid collid) int sign, bool is_to_char, Oid collid)
{ {
FormatNode *n; FormatNode *n;
NUMProc _Np, NUMProc _Np,
*Np = &_Np; *Np = &_Np;
const char *pattern; const char *pattern;
int pattern_len; size_t pattern_len;
MemSet(Np, 0, sizeof(NUMProc)); MemSet(Np, 0, sizeof(NUMProc));
@@ -5830,7 +5830,7 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout,
*/ */
if (Np->last_relevant && Np->Num->zero_end > Np->out_pre_spaces) if (Np->last_relevant && Np->Num->zero_end > Np->out_pre_spaces)
{ {
int last_zero_pos; size_t last_zero_pos;
char *last_zero; char *last_zero;
/* note that Np->number cannot be zero-length here */ /* note that Np->number cannot be zero-length here */
@@ -6222,7 +6222,7 @@ do { \
*/ */
#define NUM_TOCHAR_finish \ #define NUM_TOCHAR_finish \
do { \ do { \
int len; \ size_t len; \
\ \
NUM_processor(format, &Num, VARDATA(result), numstr, 0, out_pre_spaces, sign, true, PG_GET_COLLATION()); \ NUM_processor(format, &Num, VARDATA(result), numstr, 0, out_pre_spaces, sign, true, PG_GET_COLLATION()); \
\ \
@@ -6367,7 +6367,7 @@ numeric_to_char(PG_FUNCTION_ARGS)
} }
else else
{ {
int numstr_pre_len; size_t numstr_pre_len;
Numeric val = value; Numeric val = value;
Numeric x; Numeric x;
@@ -6464,7 +6464,7 @@ int4_to_char(PG_FUNCTION_ARGS)
} }
else else
{ {
int numstr_pre_len; size_t numstr_pre_len;
if (IS_MULTI(&Num)) if (IS_MULTI(&Num))
{ {
@@ -6573,7 +6573,7 @@ int8_to_char(PG_FUNCTION_ARGS)
} }
else else
{ {
int numstr_pre_len; size_t numstr_pre_len;
if (IS_MULTI(&Num)) if (IS_MULTI(&Num))
{ {
@@ -6687,7 +6687,7 @@ float4_to_char(PG_FUNCTION_ARGS)
{ {
float4 val = value; float4 val = value;
char *orgnum; char *orgnum;
int numstr_pre_len; size_t numstr_pre_len;
if (IS_MULTI(&Num)) if (IS_MULTI(&Num))
{ {
@@ -6799,7 +6799,7 @@ float8_to_char(PG_FUNCTION_ARGS)
{ {
float8 val = value; float8 val = value;
char *orgnum; char *orgnum;
int numstr_pre_len; size_t numstr_pre_len;
if (IS_MULTI(&Num)) if (IS_MULTI(&Num))
{ {