0

I'm trying to protect a program from invalid inputs and I've made a function that is not working in Linux but, apparently, works fine in Windows. I receive a String and the program is supposed to accept only Strings that have 3 " " and the last 3 values are digits. E.g "Mike 1 1 1".

The solution I came up with was

int verify(char string[]){
    int n = 0;
    char* aux = strtok(string, " ");
    while((aux=strtok(NULL, " "))!=NULL){
        if(isdigit(aux[0])==0)break;
        n++;
    }
    return n;
}

If n is 3 I accept the input. I know that this solution won't be correct for an input like "Mike 1 1a 1" but for now that won't be important. I really wanted to know why this doesn't work in Linux and how can I test this in a way that will work.

Mike
  • 11
  • 3
  • 2
    We need [Minimal, Complete, and Verifiable](https://stackoverflow.com/help/mcve). Are you aware that `strtok` modifies source string? You might be modifying constant string. Impossible to say - you didn't post that gets your string. –  Dec 09 '17 at 01:07
  • Possible duplicate of [Is it possible to modify a string of char in C?](https://stackoverflow.com/questions/1011455/is-it-possible-to-modify-a-string-of-char-in-c). Your edit doesn't change the problem. A parameter `char string[]` is equivalent to `char *string`. The problem is what you *pass* to `verify()`. – Déjà vu Dec 09 '17 at 01:08
  • @Ivan I just edited it right now because I wrote char* instead of char[], I believe that what is sent to this function is a copy of the original string so the copy will be modified but the original won't. – Mike Dec 09 '17 at 01:12
  • @Mike No, in C there is no difference between `char[]` and `char *`. –  Dec 09 '17 at 01:13
  • @RingØ Isn't it a copy of the original string? – Mike Dec 09 '17 at 01:13
  • No it's not a copy! `string` is a pointer to the same string you pass as parameter. Read the link given above "Is it possible to modify a string...". You have to copy the string before calling verify, or in `verify` (since you return an int) `char copy[strlen(string)+1] ; strcpy(copy, string);` then use `copy` [depends on lenght of string, it it's big, better use malloc] – Déjà vu Dec 09 '17 at 01:14
  • @mike, C doesn't do anything you don't tell it to. If you don't copy a string it doesn't make a copy! – Martin Beckett Dec 09 '17 at 01:16
  • 1
    It's working now! I didn't know that a string would get modified if I sent it through another function and thought the problem was something else since it "worked" in Windows. – Mike Dec 09 '17 at 01:32
  • @RingØ I need to ask another thing, I still get a segmentation fault when trying to use strcpy because the last value will be NULL since I'm doing strcpy(aux, strtok(NULL, " ")) and apparently strcpy can't handle it, how should I prevent this from happening? – Mike Dec 09 '17 at 01:52
  • 1
    @Mike ask another question, that includes the **code** – Déjà vu Dec 09 '17 at 01:59
  • You could just use `int o = 0, n;` and `n = -1; sscanf(string + o, "%*d%n", &n);` and `n = -1; sscanf(string + o, "%*s%n", &n);` instead. This works without modifying the string itself. The first one yields a positive `n` if the next token is a number; the second one yields a positive `n` when the next token is non-empty. Both ignore leading whitespace. – Nominal Animal Dec 09 '17 at 05:41
  • such a condition, as your question asks, is a very strong sign that the code contains some undefined behavior – user3629249 Dec 09 '17 at 11:59
  • @user3629249 I'm reading from a named pipe so the person that tests my code can send anything through the pipe. This condition will verify if it's a valid input or not. – Mike Dec 09 '17 at 16:42

0 Answers0