views:

77

answers:

3

Hi, I have c++ generated executable in solaris 8. the problem i am having is that this executable uses a cmd line param to run.

for example,

$ myprog 123412341234AB 

this is a valid 14 digit hexdecimal value. however, if for some reason there are symbols like ^ > < >> << & etc then the program does not behave properly per say. i am not talking core dumps per say but for example one of the checks i do is via isxdigit. apparently its not good enough to catch something like 1234123412341^ or 12341234(12341

so i am just trying to see if i can detect all these symbols in an effort to just exit properly. i mean some of these symbols have special meaning in unix and i guess that is why the prog does not understand how to handle it.

any thoughts on how to address this ? do i just try to find all these symbols and the moment i detect them in the cmd like i just exit out with an error message? how would i go about doing this?

i am using std::string. So maybe a list like !@#$%^&*()<><<>> etc where i can detect and get out. i am not sure if there is an easier way to do this so unix does not think i am giving it a system command when in fact its just an input to a program albeit it just happens to be a wrong/invalid input.

A: 

Just to address one part of the question, you could make a "isxstring" function like this:

bool isxstring(const std::string &s) {
    for(std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
        if(!isxdigit(*it)) {
            return false;
        }
    }
    return true;
}

If you aren't doing something at least similar to this, then you aren't properly checking if every character is a hex-digit.

This would also likely be a valid definition:

bool isxstring(const std::string &s) {
    return s.find_first_not_of("0123456789abcdefABCDEF") == std::string::npos;
}
Evan Teran
A: 

These characters are special to the command shell, and are being stripped off before your program even starts. Read up on quoting or escaping special characters for your shell.

Mark Ransom
I do wonder if he is escaping the characters to get them passed to the program since he doesn't say.
Evan Teran
i am not doing any escaping of any of those chars. I did not really anticipate this issue till now. I was happy with just checking the length/size/making sure the string is not empty and using isxdigit function. this was something i just happened to find and not sure what to do about it. i expect the third party will be just calling this exe and using the value i return in another program. but i just wanted to see if there was anything i could do in terms of error handling per say since they will not have access to source only exe.
djones2010
+3  A: 

You can't fix this by modifying your program -- those special characters are being interpreted by the shell before your code ever sees them.

You can prevent this by single-quoting the command-line argument:

myprog 'some_string_<with_special&>!chars'

or by escaping the special characters (by preceding each one with a backslash):

myprog some_string_\<with_special\&\>\!chars
Jim Lewis
yea i think the onus of responsibility to call my executable should be on their head not mine. THey should call it like that so the shell can differentiate. I should only be responsible once the value is passed into my program. For that I think the isxdigit function works fine. checking the size and almos making sure the string is not empty are just some of the checks i do. So i think that would take care of it. don't you think?
djones2010