From 8b035adde61574bd8a19703cce3a7a510ad9936a Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 7 Oct 2026 08:21:37 +0200 Subject: [PATCH v3 1/2] Change float{4,8}in_internal take const char * input argument This makes the function signature match strtof()/strtod(). A caller then doesn't have to cast away a const anymore. Reviewed-by: Rui Zhao Discussion: https://www.postgresql.org/message-id/c76b72dd-5736-453a-b698-6968d42e4768%40eisentraut.org --- contrib/postgres_fdw/postgres_fdw.c | 2 +- src/backend/utils/adt/float.c | 19 ++++++++----------- src/include/utils/float.h | 4 ++-- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index 4ca061c9e0b..6757ee923cb 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -6476,7 +6476,7 @@ set_float_arg(NullableDatum *arg, const char *s) { if (s) { - float4 val = float4in_internal((char *) s, NULL, "float", s, NULL); + float4 val = float4in_internal(s, NULL, "float", s, NULL); arg->value = Float4GetDatum(val); arg->isnull = false; diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index fd7a6587132..38ef6daf290 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -221,12 +221,12 @@ float4in(PG_FUNCTION_ARGS) * comments also apply here, except regarding use in geometric types. */ float4 -float4in_internal(char *num, char **endptr_p, +float4in_internal(const char *num, char **endptr_p, const char *type_name, const char *orig_string, struct Node *escontext) { float val; - char *endptr; + const char *endptr; /* * endptr points to the first character _after_ the sequence we recognized @@ -249,7 +249,7 @@ float4in_internal(char *num, char **endptr_p, type_name, orig_string))); errno = 0; - val = strtof(num, &endptr); + val = strtof(num, unconstify(char **, &endptr)); /* did we not see anything that looks like a double? */ if (endptr == num || errno != 0) @@ -342,7 +342,7 @@ float4in_internal(char *num, char **endptr_p, /* report stopping point if wanted, else complain if not end of string */ if (endptr_p) - *endptr_p = endptr; + *endptr_p = unconstify(char *, endptr); else if (*endptr != '\0') ereturn(escontext, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), @@ -428,17 +428,14 @@ float8in(PG_FUNCTION_ARGS) * If escontext points to an ErrorSaveContext node, that is filled instead * of throwing an error; the caller must check SOFT_ERROR_OCCURRED() * to detect errors. - * - * "num" could validly be declared "const char *", but that results in an - * unreasonable amount of extra casting both here and in callers, so we don't. */ float8 -float8in_internal(char *num, char **endptr_p, +float8in_internal(const char *num, char **endptr_p, const char *type_name, const char *orig_string, struct Node *escontext) { double val; - char *endptr; + const char *endptr; /* skip leading whitespace */ while (*num != '\0' && isspace((unsigned char) *num)) @@ -455,7 +452,7 @@ float8in_internal(char *num, char **endptr_p, type_name, orig_string))); errno = 0; - val = strtod(num, &endptr); + val = strtod(num, unconstify(char **, &endptr)); /* did we not see anything that looks like a double? */ if (endptr == num || errno != 0) @@ -544,7 +541,7 @@ float8in_internal(char *num, char **endptr_p, /* report stopping point if wanted, else complain if not end of string */ if (endptr_p) - *endptr_p = endptr; + *endptr_p = unconstify(char *, endptr); else if (*endptr != '\0') ereturn(escontext, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), diff --git a/src/include/utils/float.h b/src/include/utils/float.h index ffa743d6273..dbfbb3bf6ad 100644 --- a/src/include/utils/float.h +++ b/src/include/utils/float.h @@ -37,10 +37,10 @@ extern float8 float_overflow_error_ext(struct Node *escontext); extern float8 float_underflow_error_ext(struct Node *escontext); extern float8 float_zero_divide_error_ext(struct Node *escontext); extern int is_infinite(float8 val); -extern float8 float8in_internal(char *num, char **endptr_p, +extern float8 float8in_internal(const char *num, char **endptr_p, const char *type_name, const char *orig_string, struct Node *escontext); -extern float4 float4in_internal(char *num, char **endptr_p, +extern float4 float4in_internal(const char *num, char **endptr_p, const char *type_name, const char *orig_string, struct Node *escontext); extern char *float8out_internal(float8 num); -- 2.56.0