Does this part of C code call and check strtol() correctly?
port = strtol(argv[2], &endptr, 10);
if (argv[2] == endptr){
fprintf(stderr, "%s\n", "Invalid port number form");
return 1;
}
if (port == 0 && errno == EINVAL){
perror("strtol( )");
return 1;
}
if ((port == LONG_MAX || port == LONG_MIN) && errno == ERANGE){
perror("strtol( )");
return 1;
}
Thank you for your time 8 3282
lovecreatesbea. ..@gmail.com wrote:
Does this part of C code call and check strtol() correctly?
Reset errno before calling strtol().
errno = 0;
port = strtol(argv[2], &endptr, 10);
if (argv[2] == endptr){
fprintf(stderr, "%s\n", "Invalid port number form");
return 1;
}
if (port == 0 && errno == EINVAL){
perror("strtol( )");
return 1;
}
if ((port == LONG_MAX || port == LONG_MIN) && errno == ERANGE){
perror("strtol( )");
return 1;
}
Thank you for your time
You might want to add a test for *endptr != '\0'
If argv[2] contains "443foo", *endptr will be 'f' after the strtol()
call (and port will be 433)
On Sun, 29 Jun 2008 01:34:53 -0700, lovecreatesbea. ..@gmail.com wrote:
Does this part of C code call and check strtol() correctly?
Probably not.
port = strtol(argv[2], &endptr, 10);
strtol won't set errno to 0 if there's no error. You need to do that
yourself before calling it. I'll pretend you did set it to 0 when for the
rest.
if (argv[2] == endptr){
endptr == argv[2] only argv[2] doesn't start with a number. If it does,
for example if argv[2] is "10xab", then endptr will point to the 'x'. You
probably don't want to consider that a valid number.
fprintf(stderr, "%s\n", "Invalid port number form");
return 1;
}
if (port == 0 && errno == EINVAL){
The check may fail to compile on systems that don't define EINVAL, and on
those that do, the block will never be entered.
perror("strtol( )");
return 1;
}
if ((port == LONG_MAX || port == LONG_MIN) && errno == ERANGE){
This is not invalid but you can simplify it to checking if
errno == ERANGE. If it's set to that, you already know that
(port == LONG_MAX || port == LONG_MIN).
perror("strtol( )");
return 1;
}
Thank you for your time
lovecreatesbea. ..@gmail.com wrote:
Does this part of C code call and check strtol() correctly?
port = strtol(argv[2], &endptr, 10);
if (argv[2] == endptr){
fprintf(stderr, "%s\n", "Invalid port number form");
return 1;
}
if (port == 0 && errno == EINVAL){
perror("strtol( )");
return 1;
}
if ((port == LONG_MAX || port == LONG_MIN) && errno == ERANGE){
perror("strtol( )");
return 1;
}
Thank you for your time
Not tested yet, but here is a function I wrote a couple of days ago, to
do the same:
/**
\fn int xatoi(const char *str)
safe atoi() replacement
\remark exit on failure
*/
extern int xatoi(const char *str)
{
long l;
char *endptr;
assert(str != NULL);
errno = 0;
l = strtol(str, &endptr, 10);
/* handle EINVAL and ERANGE */
if (errno) {
fprintf(stderr,
"Error: conversion of '%s' to type int failed,
%s\n",
str,
strerror(errno) );
exit(EXIT_FAILU RE);
}
/* Not a number? */
if (endptr == str) {
fprintf(stderr,
"Error: conversion of '%s' failed, not a
number\n", str);
exit(EXIT_FAILU RE);
}
/* require next char after number to be in [ \0\n\t\r] */
assert(*endptr == '\0' || *endptr == ' ' || *endptr == '\n' ||
*endptr == '\r' || *endptr == '\t' );
/* check for over- and underflow */
assert(l <= INT_MAX);
assert(l >= INT_MIN);
return (int) l;
}
--
Tor <bw****@wvtqvm. vw | tr i-za-h a-z>
On Sun, 29 Jun 2008 01:51:34 -0700 (PDT), ba******@gmail. com wrote:
>You might want to add a test for *endptr != '\0' If argv[2] contains "443foo", *endptr will be 'f' after the strtol() call (and port will be 433)
In general (not for argv[2]) you may want to accept trailing
whitespace, e.g. because strtol accepts leading whitespace.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
On Jun 29, 5:01 pm, Harald van D?k <true...@gmail. comwrote:
On Sun, 29 Jun 2008 01:34:53 -0700, lovecreatesbea. ..@gmail.com wrote:
Does this part of C code call and check strtol() correctly?
Probably not.
port = strtol(argv[2], &endptr, 10);
strtol won't set errno to 0 if there's no error. You need to do that
yourself before calling it. I'll pretend you did set it to 0 when for the
rest.
if (argv[2] == endptr){
endptr == argv[2] only argv[2] doesn't start with a number. If it does,
for example if argv[2] is "10xab", then endptr will point to the 'x'. You
probably don't want to consider that a valid number.
fprintf(stderr, "%s\n", "Invalid port number form");
return 1;
}
if (port == 0 && errno == EINVAL){
The check may fail to compile on systems that don't define EINVAL, and on
those that do, the block will never be entered.
perror("strtol( )");
return 1;
}
if ((port == LONG_MAX || port == LONG_MIN) && errno == ERANGE){
This is not invalid but you can simplify it to checking if
errno == ERANGE. If it's set to that, you already know that
(port == LONG_MAX || port == LONG_MIN).
perror("strtol( )");
return 1;
}
Thank you. I come up with this new one.
errno = 0;
port = strtol(argv[2], &endptr, 10);
if (errno == ERANGE){
perror("strtol( ) bbb");
return 1;
}
if (endptr == argv[2]){
fprintf(stderr, "%s\n", "Invalid form");
return 1;
}
if (*endptr != '\0'){
while (endptr != argv[2] + strlen(argv[2])){
if (!isspace(*endp tr++)){
fprintf(stderr, "%s\n", "Invalid
form");
return 1;
}
}
}
On Jun 29, 7:39*pm, rpbg...@yahoo.c om (Roland Pibinger) wrote:
On Sun, 29 Jun 2008 01:51:34 -0700 (PDT), badc0...@gmail. com wrote:
You might want to add a test for *endptr != '\0'
If argv[2] contains "443foo", *endptr will be 'f' after the strtol()
call (and port will be 433)
In general (not for argv[2]) you may want to accept trailing
whitespace, e.g. because strtol accepts leading whitespace.
Only " 443 " is considered valid in the code in my last post, while
both "443foo" and "443 foo" are invalid.
On Sun, 29 Jun 2008 07:50:17 -0700 (PDT),
"lovecreatesbea ...@gmail.com" <lo************ ***@gmail.comwr ote:
snip
>Thank you. I come up with this new one.
errno = 0;
port = strtol(argv[2], &endptr, 10);
port must be at least as large as a long. If it is an int or shorter,
you could still have undefined behavior depending on the value of
argv[2].
if (errno == ERANGE){
perror("strtol( ) bbb");
return 1;
}
if (endptr == argv[2]){
fprintf(stderr, "%s\n", "Invalid form");
return 1;
}
if (*endptr != '\0'){
You don't really need this if statement. Should *endptr equal
'\0', then endptr must equal argv[2]+strlen(argv[2]). In this case,
the following while statement will evaluate to false and the loop will
not be entered.
while (endptr != argv[2] + strlen(argv[2])){
if (!isspace(*endp tr++)){
fprintf(stderr, "%s\n", "Invalid form");
return 1;
}
}
}
Remove del for email
On Jun 30, 6:01 am, Barry Schwarz <schwa...@dqel. comwrote:
On Sun, 29 Jun 2008 07:50:17 -0700 (PDT),
"lovecreatesbea ...@gmail.com" <lovecreatesbea ...@gmail.comwr ote:
errno = 0;
port = strtol(argv[2], &endptr, 10);
port must be at least as large as a long. If it is an int or shorter,
you could still have undefined behavior depending on the value of
argv[2].
Yes, it's defined as the type of the return type of strtol().
if (errno == ERANGE){
perror("strtol( ) bbb");
return 1;
}
if (endptr == argv[2]){
fprintf(stderr, "%s\n", "Invalid form");
return 1;
}
if (*endptr != '\0'){
You don't really need this if statement. Should *endptr equal
'\0', then endptr must equal argv[2]+strlen(argv[2]). In this case,
the following while statement will evaluate to false and the loop will
not be entered.
while (endptr != argv[2] + strlen(argv[2])){
if (!isspace(*endp tr++)){
fprintf(stderr, "%s\n", "Invalid
form");
return 1;
}
}
}
Thank you.
I made it have more indent with that outer if statement. Removing it
seems better. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Amadeus W.M. |
last post by:
Does strtol raise any exceptions?
Thanks!
|
by: Matthias Kluwe |
last post by:
Hi!
In C, my everyday usage of strtol looked like using
char *end;
strtol( text, &end, 10 );
to read an int/long and then checking
end == ( text + strlen( text ) );
|
by: William Payne |
last post by:
Hello, I am in the process of converting a C++ program to a C program. The
user of the program is supposed to supply an integer on the command line and
in the C++ version of the program I was using something called stringstreams
to do the conversion. Here's my C version, can I leave it as it is or does
it need to be robustified or changed in any manner, regarding error
checking?
char* endptr;
errno = 0;
|
by: nrk |
last post by:
Isn't:
char s = "--4";
char *endptr;
strtol(s, &endptr, 0);
supposed to return 0 and set endptr to s? I have run into an implementation
(not gcc, gcc does what I expect) that is returning LONG_MIN!! I am afraid
to see what it did to endptr :-)
|
by: David Scarlett |
last post by:
Another two questions...
Is behaviour defined when the first argument of strtol is NULL?
And if the string contains only digits, is the 2nd argument (assuming
it wasn't NULL) guaranteed to be set to a pointer to a pointer to '\0'?
Thanks.
| |
by: whisper |
last post by:
Hello: I am trying to write code to read in a bunch of lines from
stdin,
each containing a (variable) number of integers and writing each
integer
in a separate line to stdout.
I came up the code below.
I could not get sscanf to work because it does not increment its
position
|
by: Bore Biko |
last post by:
Dear,
I have function that transform time from secconds
to string formated as dd:hh:mm:ss
(days:hours:minutes:seconds), but it doesent
work correctly...
Here s code compiled with gcc
it goes (if you name program as "transform"),
transform 1000, for 1000 seconds...
|
by: Nicola Mezzetti |
last post by:
Greetings,
I write to ask information about how to disable the preprocessor
checks on macros when compiling with command line Borland C++ compiler.
Waiting for a reply, i thank you all for the kindness and availability.
Best regards,
nicola
--
|
by: Somebody |
last post by:
Hi,
I'm trying to write the function below and have it working, but I
benchmarked it against strcmp() and its much slower. My test is to compare 2
strings that are identitical 100,000,000 times. Yeah, thats a lot, but I
needed to scale up the test to get a real timing on the function. The weird
thing is, even when I comment out almost the entire function, its still much
slower then strcmp()... at full implementation, strcmp() takes 5...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |