Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > perror()4 says SUCCESS

Reply
Thread Tools

perror()4 says SUCCESS

 
 
arnuld
Guest
Posts: n/a
 
      11-22-2011
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
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      11-22-2011
arnuld <> 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 ___
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      11-22-2011
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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-22-2011
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
d
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-22-2011
Vincenzo Mercuri <> 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- <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      11-22-2011
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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-22-2011
On 11/22/2011 04:30 PM, Keith Thompson wrote:
> Vincenzo Mercuri <> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-22-2011
Vincenzo Mercuri <> writes:
> Keith Thompson ha scritto:
>> Vincenzo Mercuri<> 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- <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      11-22-2011
James Kuyper <> writes:

> On 11/22/2011 04:30 PM, Keith Thompson wrote:
>> Vincenzo Mercuri <> 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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-22-2011
On 11/22/2011 06:36 PM, Ben Pfaff wrote:
> James Kuyper <> writes:
>
>> On 11/22/2011 04:30 PM, Keith Thompson wrote:
>>> Vincenzo Mercuri <> 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. :-}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command of the English Language is very important to our professional success. Microsoft Certification 2 03-10-2005 06:56 PM
Success at last with one minor problem. =?Utf-8?B?SmFjaw==?= Wireless Networking 1 02-22-2005 11:09 AM
A little success but still problems =?Utf-8?B?TTc2?= Wireless Networking 14 01-18-2005 12:36 AM
SUCCESS!!! =?Utf-8?B?U3RlcGhhbmll?= Wireless Networking 0 10-20-2004 08:11 PM
Dell says no & Acronis says maybe sysprep utility ( Re: Anyone use Acronis Drive Image 7.0? Bobby Fischler Computer Support 0 07-24-2004 12:12 AM



Advertisments