0

I am making a game where the answer is stored in client_challenges->answer while the client inputs the answer (which is stored in buffer) in the following format:

A: myanswer

If the answer starts from alphabet A, then i need to compare myanswer with the answer pre-stored. Using the code below, I get the correct buffer and ans lengths but if I print out my store array and answer array, the results differ. For example, if I input A: color, my store gives colo instead of color. However, store-2 works in some cases. How can I fix this?

if (buffer[0] == 'A')
{
        printf("ans len %ld, buff len %ld\n",strlen(client_challenges->answer,(strlen(buffer)-4));
        if(strlen(client_challenges->answer) == (strlen(buffer)-4))
        {
            char store[100];
            for (int i = 1; i<= strlen(client_challenges->answer);i++)
            {
                store[i-1]=buffer[2+i];
            }
            store[strlen(store)-2] = '\0';
            //store[strlen(client_challenges->answer)+1]='\0';
            printf("Buffer: <%s>\n", buffer);
            printf("STORE: %s\n",store);
            printf("ANSWER: %s\n",client_challenges->answer);
            if(strcmp(store,client_challenges->answer)==0)
               {
                send(file_descriptor, correct, strlen(correct), 0);
               }
       }
}

Example: Client enters

A: Advancement

ans len 11, buff len 11 But when I print out store, it is Advancemen while the answer is Advancement. However, in my previous attempt, answer was soon and I entered "soon". It worked then.

13
  • I don't know how you are filling your buffer. But maybe sometimes you have a new line at the end and sometimes you don't? Maybe you need to know what the last character is before you store[strlen(store)-2] = '\0'; Commented Nov 17, 2019 at 20:37
  • @JerryJeremiah i'm using this: valread = read(file_descriptor, buffer, 1024); On the command line, I just enter A: color and then hit enter in all cases. then the server tells me if i am wrong or not.
    – user12252038
    Commented Nov 17, 2019 at 20:49
  • 1
    Not sure how this is compiling there is a ) missing after the first strlen: printf("ans len %ld, buff len %ld\n",strlen(client_challenges->answer),(strlen(buffer)-4)); Commented Nov 17, 2019 at 21:14
  • 1
    I'm generating random words in the program. This time I tried with "Many". Buffer gives me "<A: Many". Answer gives me "Many" while store gives me "Many?" wher e the last character is a special one. See the edited code to check where exactly I printed them @chux-ReinstateMonica
    – user12252038
    Commented Nov 17, 2019 at 21:19
  • 1
    Suggested printf() contained sentinel characters <>. The "<A: Many" report seems wrong. store output with sentinels would have helped rather than just "Many?". Recommend,prior to if (buffer[0] == 'A'), code does buffer[strcspn(buffer, "\r\n")] = 0; to get rid of line endings and also adjust your offset accordingly. Good luck.
    – chux
    Commented Nov 17, 2019 at 21:24

1 Answer 1

0

Although I can not pin point the exact reason of this bug with the given input, I can share my experiences about how to find the correct spot efficiently.

  1. Always verify your input.

Never trust an input. You only printed out the lengths of the inputs, what is the content. You'd better check with every byte (preferably in hex) to spot not printable characters. Some IDE provide integrated debugger to show buffer contents.

  1. Use defines, constants, some human readable things instead of 4 or 2. This makes life much easier For instance

        /* what is 4 here */
        strlen(buffer)-4
    

should have been:

    /* remove 'A: ' (A, colon, and white space, and I do not know what is 4th item */
    strlen(buffer) - USER_ADDED_HEADERS
  1. Get more familiar with C library

You actually did not need store array here. C provides strncmp function to compare two strings up to size "n" or memcmp to compare two buffers. This would save copy operation (cpu cycles), and stack memory.

More clear version of your code fragment (without error checks) could have been written as:

if (buffer[0] == 'A') {
    /* verify input here */
    /* #define ANSWER_START 4 // I do not know what the 4 is */

    /* compare lengths here if they are not equal return sth accordingly */

    /* supplied answer correct? */
    if (memcmp(client_challenges->answer, 
                buffer + ANSWER_START, 
                strlen(client_challenges->answer)) == 0) {
        /* do whatever you want here */
    }
}
  1. Consistent code formatting

Code formatting DOES matter. Be consistent on indents, curly parenthesis, tabs vs spaces, spaces before/after atoms etc. You do not have to stick to one format, but you have to be consistent.

  1. Use a debugger

Debugger is your best friend. Learn about it. The issue with this bug can be identified with the debugger very easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.