pg_atoi()

Started by Richard Harvey Chapmanalmost 26 years ago2 messagesgeneral
Jump to latest
#1Richard Harvey Chapman
hchapman@3gfp.com

Is there a reason why pg_atoi() was programmed to fail if the entire
input string is not valid?
i.e. "109" yields 109, but "109 apples" yields an error.

Snippet from pg_atoi() in src/backend/utils/adt/numutils.c:

l = strtol(s, &badp, 10);

if (errno) /* strtol must set ERANGE */
elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
if (badp && *badp && (*badp != c))
elog(ERROR, "pg_atoi: error in \"%s\": can\'t parse
\"%s\"", s, badp);

Thanks,

R.

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Richard Harvey Chapman (#1)
Re: pg_atoi()

Richard Harvey Chapman <hchapman@3gfp.com> writes:

Is there a reason why pg_atoi() was programmed to fail if the entire
input string is not valid?
i.e. "109" yields 109, but "109 apples" yields an error.

Because that's what it ought to do, if you ask me ;-).
"109 foo" is not a valid integer value. If you want your app to
accept such things, do your own input parsing and filtering.
A database server should not be lax about what it considers valid
data.

regards, tom lane