linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "J." <mailing-lists@xs4all.nl>
To: linux-c-programming@vger.kernel.org
Subject: Re: strtod() and errno question.
Date: Wed, 11 Jan 2006 12:24:14 +0100 (CET)	[thread overview]
Message-ID: <Pine.LNX.4.21.0601111217280.995-100000@hestia> (raw)
In-Reply-To: <17348.29957.526334.352861@cerise.gclements.plus.com>

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;
}


      reply	other threads:[~2006-01-11 11:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.21.0601111217280.995-100000@hestia \
    --to=mailing-lists@xs4all.nl \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).