LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-04-2017, 11:40 PM   #16
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,526
Blog Entries: 6

Rep: Reputation: 2435Reputation: 2435Reputation: 2435Reputation: 2435Reputation: 2435Reputation: 2435Reputation: 2435Reputation: 2435Reputation: 2435Reputation: 2435Reputation: 2435

Quote:
Originally Posted by BW-userx View Post
unsigned char and just char what I read is the same thing I googled it.
Code:
there is no difference. Actually on many platforms unqualified char is signed.
 The same way -- e.g. if you have an 8-bit char, 7 bits can be used for magnitude
  and 1 for sign. So an unsigned char might range from 0 to 255,
  whilst a signed char might range from -128 to 127 (for example)
neither one is a digit so isdigit should return a non zero
But you missed the point, or I did not make it clear.

First, isdigit() does not check the type (i.e. signed, unsigned, int or char).
Code:
DESCRIPTION
       These functions check whether c, which must have the value of an unsigned char or EOF,
       falls into a certain character class according to the current locale.
It checks whether the encoded character represents a digit in the locale, usually ascii for our purposes.

My point was not signed vs unsigned char or int, it was char or int, vs pointer to char.

argv[optind] is always a pointer to a character array, so with...

Code:
isdigit(argv[optind])
...you are passing a pointer, not a char or an int. char is defined as the smallest representation of a character on any system, and pointer is always the largest integer type. Hence the source of memory corruption and segfault.

This may also be obscured a bit if you are not including ctype.h. Then isdigit is probably inlined using a gcc macro so it results in no errors. Using -Wall as previously mentioned may show a warning at compile time.

Next, when you call it this way, you are passing it an integer, that is a number, not a digit encoded as a character.

Code:
isdigit(atoi(argv[optind]))
So it does not segfault, but it is no longer a valid test of the character encoding and your test fails.

Dereferencing argv[optind] passed the character (i.e. ascii value) and tests whether the first character of the argument is an encoded digit...

Code:
isdigit(*argv[optind])
Note that it does not test the numeric value of multi-character arguments, only the first character of the argument string. So if the argument is "32", this only tests the "3". If the argument is "3X" it passes the test too!
 
Old 08-05-2017, 09:55 AM   #17
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,742

Original Poster
Rep: Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877
Quote:
Originally Posted by astrogeek View Post
But you missed the point, or I did not make it clear.

First, isdigit() does not check the type (i.e. signed, unsigned, int or char).
Code:
DESCRIPTION
       These functions check whether c, which must have the value of an unsigned char or EOF,
       falls into a certain character class according to the current locale.
It checks whether the encoded character represents a digit in the locale, usually ascii for our purposes.

My point was not signed vs unsigned char or int, it was char or int, vs pointer to char.

argv[optind] is always a pointer to a character array, so with...

Code:
isdigit(argv[optind])
...you are passing a pointer, not a char or an int. char is defined as the smallest representation of a character on any system, and pointer is always the largest integer type. Hence the source of memory corruption and segfault.

This may also be obscured a bit if you are not including ctype.h. Then isdigit is probably inlined using a gcc macro so it results in no errors. Using -Wall as previously mentioned may show a warning at compile time.

Next, when you call it this way, you are passing it an integer, that is a number, not a digit encoded as a character.

Code:
isdigit(atoi(argv[optind]))
So it does not segfault, but it is no longer a valid test of the character encoding and your test fails.

Dereferencing argv[optind] passed the character (i.e. ascii value) and tests whether the first character of the argument is an encoded digit...

Code:
isdigit(*argv[optind])
Note that it does not test the numeric value of multi-character arguments, only the first character of the argument string. So if the argument is "32", this only tests the "3". If the argument is "3X" it passes the test too!
I went back and looked in to isdigit then put I to the test myself as just the name in itself is misleading.

when I read on it I was only looking for return value making sure true was zero so I could use the return value as a means for error checking, handling not input has to be an int to begin with, which pretty much makes it useless.

so even using isdigit( atoi(char) ) is valid because it is now giving isdigit what it needs for input with is an int. But still will not tell if it is a a - z or 1 - 0 ..

needless to say I as just running when I should have been walking is all.

Last edited by BW-userx; 08-05-2017 at 10:11 AM.
 
Old 08-07-2017, 11:01 AM   #18
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,904

Rep: Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921
for checking how isdigit really works you can use ddd (The Data Display Debugger). Just compile your code with -g and without -O and execute your code inside the debugger.
Code:
// splittting this: isdigit(atoi(argv[optind])) into:
a = argv[optind]
b = atoi(a)
c = isdigit(b)
will make easier to understand what is passed to which function and what is the result.
And of course if you don't want to debug you can printf all these variables to stdout (just need to take care about the real types).
 
Old 08-07-2017, 01:44 PM   #19
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,742

Original Poster
Rep: Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877
Quote:
Originally Posted by pan64 View Post
for checking how isdigit really works you can use ddd (The Data Display Debugger). Just compile your code with -g and without -O and execute your code inside the debugger.
Code:
// splittting this: isdigit(atoi(argv[optind])) into:
a = argv[optind]
b = atoi(a)
c = isdigit(b)
will make easier to understand what is passed to which function and what is the result.
And of course if you don't want to debug you can printf all these variables to stdout (just need to take care about the real types).
What??? GDB has a gui now -> The Data Display Debugger.

that will be interesting to check in to. Because yeah I've been using printf everywhere to see if the flow of data is going where it needs to be and what is the values of them when and if it gets there.

.... mod:

... and its already installed, I didn't even know I had it until I did a ddd on the cli -- wow -- thanks!

Last edited by BW-userx; 08-07-2017 at 01:53 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a standard way to differentiate command line arguments and flags in C Alpha90 Programming 11 03-10-2016 10:27 PM
BASH script using getopt to parse optional arguments nano2 General 6 04-28-2011 09:09 AM
how is zero flags set telnet_ping Programming 5 09-14-2007 09:06 PM
How do you set compiler flags in SuSE? Thaidog SUSE / openSUSE 0 12-26-2006 11:04 PM
flags vs normal arguments merchtemeagle Programming 7 12-14-2005 11:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration