* strtod() and errno question.
@ 2006-01-10 21:42 J.
2006-01-10 22:34 ` James Stevenson
2006-01-11 3:01 ` Glynn Clements
0 siblings, 2 replies; 4+ messages in thread
From: J. @ 2006-01-10 21:42 UTC (permalink / raw)
To: linux-c-programming
Tuesday, January 10 22:35:50
Hi people,
I have a question regarding errno and strtod() error checking. I have a
function [listed below] that extracts a number from a string. In case of
an error, strerror() in conjunction with errno is used to display a user
friendly error message. However on a strtod() error; `Success' is
returned.
Error - strtod(ca4)`c': Success
Since I am a bit confused by the strtod() manual page, what would be the
correct way to handle this ?
Thankx !
J.
int extrnumber(char *str) {
int retv = 0;
char *nptr = NULL;
retv = strtod(str, &nptr);
if(retv == 0 && nptr != NULL) {
fprintf(stderr, "%s: Error - strtod(%s)`%c': %s\n", PACKAGE,
str, *nptr, strerror(errno));
exit(EXIT_FAILURE);
}
return retv;
}
^ permalink raw reply [flat|nested] 4+ messages in thread* RE: strtod() and errno question.
2006-01-10 21:42 strtod() and errno question J.
@ 2006-01-10 22:34 ` James Stevenson
2006-01-11 3:01 ` Glynn Clements
1 sibling, 0 replies; 4+ messages in thread
From: James Stevenson @ 2006-01-10 22:34 UTC (permalink / raw)
To: linux-c-programming
Hi,
Try taking a copy of errno before calling fprintf.
See if that helps any.
James
> -----Original Message-----
> From: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-
> programming-owner@vger.kernel.org] On Behalf Of J.
> Sent: 10 January 2006 21:43
> To: linux-c-programming@vger.kernel.org
> Subject: strtod() and errno question.
>
> Tuesday, January 10 22:35:50
>
> Hi people,
>
> I have a question regarding errno and strtod() error checking. I have a
> function [listed below] that extracts a number from a string. In case of
> an error, strerror() in conjunction with errno is used to display a user
> friendly error message. However on a strtod() error; `Success' is
> returned.
>
> Error - strtod(ca4)`c': Success
>
> Since I am a bit confused by the strtod() manual page, what would be the
> correct way to handle this ?
>
> Thankx !
>
> J.
>
> int extrnumber(char *str) {
> int retv = 0;
> char *nptr = NULL;
>
> retv = strtod(str, &nptr);
> if(retv == 0 && nptr != NULL) {
> fprintf(stderr, "%s: Error - strtod(%s)`%c': %s\n", PACKAGE,
> str, *nptr, strerror(errno));
> exit(EXIT_FAILURE);
> }
>
> return retv;
> }
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-
> programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: strtod() and errno question.
2006-01-10 21:42 strtod() and errno question J.
2006-01-10 22:34 ` James Stevenson
@ 2006-01-11 3:01 ` Glynn Clements
2006-01-11 11:24 ` J.
1 sibling, 1 reply; 4+ messages in thread
From: Glynn Clements @ 2006-01-11 3:01 UTC (permalink / raw)
To: linux-c-programming
J. wrote:
> I have a question regarding errno and strtod() error checking. I have a
> function [listed below] that extracts a number from a string. In case of
> an error, strerror() in conjunction with errno is used to display a user
> friendly error message. However on a strtod() error; `Success' is
> returned.
Note that strtod() only sets errno on overflow or underflow.
If you want to check for failure to convert (because no prefix of the
string is a valid floating-point number), you need to check whether
"nptr == str".
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: strtod() and errno question.
2006-01-11 3:01 ` Glynn Clements
@ 2006-01-11 11:24 ` J.
0 siblings, 0 replies; 4+ messages in thread
From: J. @ 2006-01-11 11:24 UTC (permalink / raw)
To: linux-c-programming
On Wed, 11 Jan 2006, Glynn Clements wrote:
> J. wrote:
>
> > I have a question regarding errno and strtod() error checking. I have a
> > function [listed below] that extracts a number from a string. In case of
> > an error, strerror() in conjunction with errno is used to display a user
> > friendly error message. However on a strtod() error; `Success' is
> > returned.
>
> Note that strtod() only sets errno on overflow or underflow.
>
> If you want to check for failure to convert (because no prefix of the
> string is a valid floating-point number), you need to check whether
> "nptr == str".
>
> Glynn Clements <glynn@gclements.plus.com>
Hi,
Thank you for all your answers. At first glance extracting a number from a
string seems rather easy, but it can be a hassle. I checked the source code
of some other programs and I changed the function to the following. I hope
that will suffice validating user input.
Thankx again.
J.
#include <limits.h>
int extrnumber(char *str) {
long lval = 0;
int retv = 0;
char *nptr = NULL;
lval = strtol(str, &nptr, 10);
if(str == '\0' || *nptr != '\0') {
fprintf(stderr, "%s: Error - conversion(%s): strtol(`%c')\n",
PACKAGE, str, *nptr);
exit(EXIT_FAILURE);
}
if((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
(lval > INT_MAX || lval < INT_MIN)) {
fprintf(stderr, "%s: Error - strtol(%s): %s\n",
PACKAGE, str, strerror(errno));
exit(EXIT_FAILURE);
}
retv = lval;
return retv;
}
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-01-11 11:24 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-01-10 21:42 strtod() and errno question J.
2006-01-10 22:34 ` James Stevenson
2006-01-11 3:01 ` Glynn Clements
2006-01-11 11:24 ` J.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).