Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #13776 > unrolled thread
| Started by | arnuld <sunrise@invalid.address> |
|---|---|
| First post | 2011-11-22 10:39 +0000 |
| Last post | 2011-11-22 20:40 -0500 |
| Articles | 16 — 9 participants |
Back to article view | Back to comp.lang.c
perror()4 says SUCCESS arnuld <sunrise@invalid.address> - 2011-11-22 10:39 +0000
Re: perror()4 says SUCCESS jt@toerring.de (Jens Thoms Toerring) - 2011-11-22 11:44 +0000
Re: perror()4 says SUCCESS Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-22 04:15 -0800
Re: perror()4 says SUCCESS Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-22 07:51 -0500
Re: perror()4 says SUCCESS Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-11-22 16:51 +0100
Re: perror()4 says SUCCESS Keith Thompson <kst-u@mib.org> - 2011-11-22 13:30 -0800
Re: perror()4 says SUCCESS Harald van Dijk <truedfx@gmail.com> - 2011-11-22 13:35 -0800
Re: perror()4 says SUCCESS Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-11-22 22:51 +0100
Re: perror()4 says SUCCESS James Kuyper <jameskuyper@verizon.net> - 2011-11-22 16:43 -0500
Re: perror()4 says SUCCESS Ben Pfaff <blp@cs.stanford.edu> - 2011-11-22 15:36 -0800
Re: perror()4 says SUCCESS James Kuyper <jameskuyper@verizon.net> - 2011-11-22 18:52 -0500
Re: perror()4 says SUCCESS Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-11-22 22:50 +0100
Re: perror()4 says SUCCESS Keith Thompson <kst-u@mib.org> - 2011-11-22 13:57 -0800
Re: perror()4 says SUCCESS Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-11-22 23:05 +0100
Re: perror()4 says SUCCESS Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-11-22 23:26 +0100
Re: perror()4 says SUCCESS Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-22 20:40 -0500
| From | arnuld <sunrise@invalid.address> |
|---|---|
| Date | 2011-11-22 10:39 +0000 |
| Subject | perror()4 says SUCCESS |
| Message-ID | <4ecb7bcf$0$292$14726298@news.sunsite.dk> |
OJECTIVE: Why does perror() says SUCESSS ?
This code converts a string like "1234" into an unsingned long integer. I
wrote it following the discussion here on clc of which I have lost the
url from google http interface to newsgroup.
perror() reports error in else condition many times, I can't understand
why:
int convert_string_to_ulongint(const char* str, unsigned long* ulp)
{
int ret;
unsigned long int num;
char* p;
if(NULL == str || '\0' == *str || NULL == ulp)
{
printf("IN: %s @ %d: Invalid Args\n", __func__, __LINE__);
return VAL_ERR;
}
p = NULL;
errno = 0;
num = strtoul(str, &p, 10);
/* Error check */
if(ERANGE == errno)
{
if((0 == num) && (0 == strcmp(str, p)))
{
printf("IN: %s @%d: strtoul() could not perform conversion\n",
__func__, __LINE__);
}
else if(ULONG_MAX == num)
{
printf("IN: %s @%dp: strtoul() overflow error\n", __func__,
__LINE__);
}
else
{
printf("IN: %s @%d: strange output from strtoul()\n", __func__,
__LINE__);
}
perror("*ERROR ERANGE*");
ret = -1;
}
else if((0 == errno) && ('\0' == *p))
{
printf("IN: %s @%d: Successful Conversion by strtoul()\n",
__func__, __LINE__);
*ulp = num;
ret = 1;
}
else
{
printf("IN: %s @%d: strange conversions and errno values :(\n",
__func__, __LINE__);
printf("num = %lu, ULONG_MAX = %lu\n", num, ULONG_MAX);
perror("*ERROR in ELSE");
ret = -1;
}
--
arnuld
http://LispMachine.Wordpress.com
[toc] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2011-11-22 11:44 +0000 |
| Message-ID | <9j1g99FufbU1@mid.uni-berlin.de> |
| In reply to | #13776 |
arnuld <sunrise@invalid.address> wrote:
> OJECTIVE: Why does perror() says SUCESSS ?
> This code converts a string like "1234" into an unsingned long integer. I
> wrote it following the discussion here on clc of which I have lost the
> url from google http interface to newsgroup.
> perror() reports error in else condition many times, I can't understand
> why:
> int convert_string_to_ulongint(const char* str, unsigned long* ulp)
> {
> int ret;
> unsigned long int num;
> char* p;
> if(NULL == str || '\0' == *str || NULL == ulp)
> {
> printf("IN: %s @ %d: Invalid Args\n", __func__, __LINE__);
> return VAL_ERR;
> }
> p = NULL;
> errno = 0;
> num = strtoul(str, &p, 10);
> /* Error check */
> if(ERANGE == errno)
> {
> if((0 == num) && (0 == strcmp(str, p)))
> {
> printf("IN: %s @%d: strtoul() could not perform conversion\n",
> __func__, __LINE__);
> }
For this ('num' being 0 and str and p pointing to the same location,
so you could actually use 'str == p' instead of strcmp()) errno
doesn't get set to ERANGE - errno gets set to ERANGE only if there
is a number to convert but it would overflow. Thus if you pass the
function a string that does not start with a number (after possible
white-space) this error isn't caught here but ends up being handled
in the very last 'else'.
> else if(ULONG_MAX == num)
Everything else than this would mean your libc is broken...
> {
> printf("IN: %s @%dp: strtoul() overflow error\n", __func__,
> __LINE__);
> }
> else
> {
> printf("IN: %s @%d: strange output from strtoul()\n", __func__,
> __LINE__);
> }
> perror("*ERROR ERANGE*");
> ret = -1;
> }
> else if((0 == errno) && ('\0' == *p))
> {
> printf("IN: %s @%d: Successful Conversion by strtoul()\n",
> __func__, __LINE__);
> *ulp = num;
> ret = 1;
> }
> else
> {
> printf("IN: %s @%d: strange conversions and errno values :(\n",
> __func__, __LINE__);
> printf("num = %lu, ULONG_MAX = %lu\n", num, ULONG_MAX);
> perror("*ERROR in ELSE");
Well, when you get here 'errno' is rather likely 0, and the
reason you're ending up here being that 'str' points to some-
thing that can't be interpreted as a number (or has trailing
parts that aren't a number which you also seem to consider as
being an error). And then perror() can't tell you anything else
than 'SUCCESS' because it interprets the value of 'errno'. So,
unless 'errno' isn't 0 it doesn't make to much sense calling
perror().
> ret = -1;
> }
I'd recommend that you change the logic a bit:
a) Check first for 'errno == 0 && *p == '\0''. That shows that
the conversion was successful.
b) If 'errno' is set to ERANGE report an overflow.
c) Otherwise something must have been wrong about the input
string: it either couldn't be converted at all (then 'p'
and 'str' will point to the same location) or there was
some trailing stuff that couldn't be interpreted as a number
(in that case you have '*p != '\0').
Another small niggle: Functions shouldn't change errno if they
succeeded. Thus I would recoment to store the original value of
errno before you set errno to 0 and then call strtoul() and reset
errno to its original value if the conversion was successful.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
[toc] | [prev] | [next] | [standalone]
| From | Nick Keighley <nick_keighley_nospam@hotmail.com> |
|---|---|
| Date | 2011-11-22 04:15 -0800 |
| Message-ID | <c6932794-b377-42c1-9ff9-99fe1db64618@m7g2000vbc.googlegroups.com> |
| In reply to | #13776 |
please include the subject in the body of your post
Subject: "perror()4 says SUCCESS Options"
I don't understand what this means
:-)
On Nov 22, 10:39 am, arnuld <sunr...@invalid.address> wrote:
> OJECTIVE: Why does perror() says SUCESSS ?
there wasn't an error?
> This code converts a string like "1234" into an unsingned long integer. I
> wrote it following the discussion here on clc of which I have lost the
> url from google http interface to newsgroup.
>
> perror() reports error in else condition many times,
under what circumstances? I ran a slightly hacked version of your code
and there was no problem.
<snip your code>
<insert my code>
******************************************************************
/* arn.c */
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define __func__ "convert_string_to_ulongint"
#define VAL_ERR ERANGE
int convert_string_to_ulongint(const char* str, unsigned long* ulp)
{
int ret;
unsigned long int num;
char* p;
if(NULL == str || '\0' == *str || NULL == ulp)
{
printf("IN: %s @ %d: Invalid Args\n", __func__, __LINE__);
return VAL_ERR;
}
p = NULL;
errno = 0;
num = strtoul(str, &p, 10);
/* Error check */
if(ERANGE == errno)
{
if((0 == num) && (0 == strcmp(str, p)))
{
printf("IN: %s @%d: strtoul() could not perform conversion
\n", __func__, __LINE__);
}
else if(ULONG_MAX == num)
{
printf("IN: %s @%dp: strtoul() overflow error\n", __func__,
__LINE__);
}
else
{
printf("IN: %s @%d: strange output from strtoul()\n",
__func__, __LINE__);
}
perror("*ERROR ERANGE*");
ret = -1;
}
else if((0 == errno) && ('\0' == *p))
{
printf("IN: %s @%d: Successful Conversion by strtoul()\n",
__func__, __LINE__);
*ulp = num;
ret = 1;
}
else
{
printf("IN: %s @%d: strange conversions and errno values :(\n",
__func__, __LINE__);
printf("num = %lu, ULONG_MAX = %lu\n", num, ULONG_MAX);
perror("*ERROR in ELSE");
ret = -1;
}
return ret;
}
int main (void)
{
unsigned long result;
int retval;
if ((retval = convert_string_to_ulongint("1234", &result)) == 1)
{
printf ("conversion succeeded result was %u\n", result);
return 0;
}
else
{
printf ("conversion FAILED error flag was %d\n", retval);
return EXIT_FAILURE;
}
return 0;
}
******************************************************************
</insert my code>
Output:-
> arn.exe
IN: convert_string_to_ulongint @52: Successful Conversion by strtoul()
conversion succeeded result was 1234
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-11-22 07:51 -0500 |
| Message-ID | <jag5sj$hpe$1@dont-email.me> |
| In reply to | #13776 |
On 11/22/2011 5:39 AM, arnuld wrote:
> OJECTIVE: Why does perror() says SUCESSS ?
>
> if(ERANGE == errno)
> {
>[... eventually call printf() ...]
> }
> perror("*ERROR ERANGE*");
If the penny still hasn't dropped, study this phrase from 7.5p3:
"The value of errno may be set to nonzero by a library function call
whether or not there is an error ..."
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> |
|---|---|
| Date | 2011-11-22 16:51 +0100 |
| Message-ID | <8LqdnR2ZJ69qWVbTnZ2dnUVZ7oednZ2d@giganews.com> |
| In reply to | #13776 |
arnuld ha scritto:
[...]
> int convert_string_to_ulongint(const char* str, unsigned long* ulp)
> {
> int ret;
> unsigned long int num;
> char* p;
int err;
[...]
> p = NULL;
> errno = 0;
> num = strtoul(str,&p, 10);
err = errno; /* read Eric's reply */
[...]
> perror("*ERROR in ELSE");
[...]
Use printf("Error: %s\n", strerror(err)); instead or the Posix
compliant and thread safe "strerror_r()".
--
Vincenzo Mercuri | www.backbox.org
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-11-22 13:30 -0800 |
| Message-ID | <lnzkfnu9oi.fsf@nuthaus.mib.org> |
| In reply to | #13785 |
Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> writes:
> arnuld ha scritto:
[...]
>> perror("*ERROR in ELSE");
> [...]
>
> Use printf("Error: %s\n", strerror(err)); instead or the Posix
> compliant and thread safe "strerror_r()".
What's the advantage of that over perror()?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Harald van Dijk <truedfx@gmail.com> |
|---|---|
| Date | 2011-11-22 13:35 -0800 |
| Message-ID | <55c6a171-6c54-40fd-b8ca-8eeb74ef3e5d@n6g2000vbg.googlegroups.com> |
| In reply to | #13798 |
On Nov 22, 10:30 pm, Keith Thompson <ks...@mib.org> wrote:
> Vincenzo Mercuri <vincenzo.merc...@yahoo.it> writes:
> > arnuld ha scritto:
> [...]
> >> perror("*ERROR in ELSE");
> > [...]
>
> > Use printf("Error: %s\n", strerror(err)); instead or the Posix
> > compliant and thread safe "strerror_r()".
>
> What's the advantage of that over perror()?
That it prints the error message associated with error code err,
rather than error code errno. That was the point of the other two
modifications in the message you replied to: to save the old value of
errno.
[toc] | [prev] | [next] | [standalone]
| From | Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> |
|---|---|
| Date | 2011-11-22 22:51 +0100 |
| Message-ID | <-5mdnS8ZeK7xhFHTnZ2dnUVZ8h6dnZ2d@giganews.com> |
| In reply to | #13799 |
Harald van Dijk ha scritto:
> On Nov 22, 10:30 pm, Keith Thompson<ks...@mib.org> wrote:
>> Vincenzo Mercuri<vincenzo.merc...@yahoo.it> writes:
>>> arnuld ha scritto:
>> [...]
>>>> perror("*ERROR in ELSE");
>>> [...]
>>
>>> Use printf("Error: %s\n", strerror(err)); instead or the Posix
>>> compliant and thread safe "strerror_r()".
>>
>> What's the advantage of that over perror()?
>
> That it prints the error message associated with error code err,
> rather than error code errno. That was the point of the other two
> modifications in the message you replied to: to save the old value of
> errno.
Yes, exactly.
--
Vincenzo Mercuri | www.backbox.org
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-11-22 16:43 -0500 |
| Message-ID | <4ECC176A.4020503@verizon.net> |
| In reply to | #13798 |
On 11/22/2011 04:30 PM, Keith Thompson wrote:
> Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> writes:
>> arnuld ha scritto:
> [...]
>>> perror("*ERROR in ELSE");
>> [...]
>>
>> Use printf("Error: %s\n", strerror(err)); instead or the Posix
>> compliant and thread safe "strerror_r()".
>
> What's the advantage of that over perror()?
It goes to stdin rather than stderr; though I'm not sure that counts as
and advantage; if stderr is different from stdout, I generally want
messages of this type going to stderr, not stdout. I guess that
Vincenzo's preferences are different.
[toc] | [prev] | [next] | [standalone]
| From | Ben Pfaff <blp@cs.stanford.edu> |
|---|---|
| Date | 2011-11-22 15:36 -0800 |
| Message-ID | <87y5v791ba.fsf@blp.benpfaff.org> |
| In reply to | #13800 |
James Kuyper <jameskuyper@verizon.net> writes:
> On 11/22/2011 04:30 PM, Keith Thompson wrote:
>> Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> writes:
>>> arnuld ha scritto:
>> [...]
>>>> perror("*ERROR in ELSE");
>>> [...]
>>>
>>> Use printf("Error: %s\n", strerror(err)); instead or the Posix
>>> compliant and thread safe "strerror_r()".
>>
>> What's the advantage of that over perror()?
>
> It goes to stdin rather than stderr; [...]
No.
--
Ben Pfaff
http://benpfaff.org
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-11-22 18:52 -0500 |
| Message-ID | <4ECC35A7.5060703@verizon.net> |
| In reply to | #13806 |
On 11/22/2011 06:36 PM, Ben Pfaff wrote:
> James Kuyper <jameskuyper@verizon.net> writes:
>
>> On 11/22/2011 04:30 PM, Keith Thompson wrote:
>>> Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> writes:
>>>> arnuld ha scritto:
>>> [...]
>>>>> perror("*ERROR in ELSE");
>>>> [...]
>>>>
>>>> Use printf("Error: %s\n", strerror(err)); instead or the Posix
>>>> compliant and thread safe "strerror_r()".
>>>
>>> What's the advantage of that over perror()?
>>
>> It goes to stdin rather than stderr; [...]
>
> No.
Aaagh! Of course I meant stdout, not stdin. Just pretend you didn't see
that. :-}
[toc] | [prev] | [next] | [standalone]
| From | Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> |
|---|---|
| Date | 2011-11-22 22:50 +0100 |
| Message-ID | <-5mdnSwZeK6whFHTnZ2dnUVZ8h6dnZ2d@giganews.com> |
| In reply to | #13798 |
Keith Thompson ha scritto:
> Vincenzo Mercuri<vincenzo.mercuri@yahoo.it> writes:
>> arnuld ha scritto:
> [...]
>>> perror("*ERROR in ELSE");
>> [...]
>>
>> Use printf("Error: %s\n", strerror(err)); instead or the Posix
>> compliant and thread safe "strerror_r()".
>
> What's the advantage of that over perror()?
>
The point I was making was that while "perror()" prints the string
representation of the *current* error described by "errno", with
"strerror()" I can print out an error message that refers to a
previously stored value of errno, such as "err" in this case.
--
Vincenzo Mercuri | www.backbox.org
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-11-22 13:57 -0800 |
| Message-ID | <lnvcqbu8fd.fsf@nuthaus.mib.org> |
| In reply to | #13801 |
Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> writes:
> Keith Thompson ha scritto:
>> Vincenzo Mercuri<vincenzo.mercuri@yahoo.it> writes:
>>> arnuld ha scritto:
>> [...]
>>>> perror("*ERROR in ELSE");
>>> [...]
>>>
>>> Use printf("Error: %s\n", strerror(err)); instead or the Posix
>>> compliant and thread safe "strerror_r()".
>>
>> What's the advantage of that over perror()?
>>
>
> The point I was making was that while "perror()" prints the string
> representation of the *current* error described by "errno", with
> "strerror()" I can print out an error message that refers to a
> previously stored value of errno, such as "err" in this case.
Got it, thanks. (I'd still prefer to print the message to stderr rather
than stdout; that's easy enough to do.)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> |
|---|---|
| Date | 2011-11-22 23:05 +0100 |
| Message-ID | <3vKdnXoxssgTgVHTnZ2dnUVZ8sednZ2d@giganews.com> |
| In reply to | #13803 |
Keith Thompson ha scritto: > Vincenzo Mercuri<vincenzo.mercuri@yahoo.it> writes: [...] >> The point I was making was that while "perror()" prints the string >> representation of the *current* error described by "errno", with >> "strerror()" I can print out an error message that refers to a >> previously stored value of errno, such as "err" in this case. > > Got it, thanks. (I'd still prefer to print the message to stderr rather > than stdout; that's easy enough to do.) > Yes, printing the message to stderr is preferable. :) -- Vincenzo Mercuri | www.backbox.org
[toc] | [prev] | [next] | [standalone]
| From | Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> |
|---|---|
| Date | 2011-11-22 23:26 +0100 |
| Message-ID | <hoSdnVm_A88fvFHTnZ2dnUVZ8qKdnZ2d@giganews.com> |
| In reply to | #13785 |
Vincenzo Mercuri ha scritto:
[...]
>
> Use printf("Error: %s\n", strerror(err)); instead or the Posix
> compliant and thread safe "strerror_r()".
>
Preferably: fprintf(stderr, "Error: %s\n", strerror(err));
--
Vincenzo Mercuri | www.backbox.org
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-11-22 20:40 -0500 |
| Message-ID | <jahiu1$o1d$1@dont-email.me> |
| In reply to | #13776 |
On 11/22/2011 5:39 AM, arnuld wrote:
> OJECTIVE: Why does perror() says SUCESSS ?
Because it takes elocution lessons from Gollum.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web