Thread: Program output is strange characters, totally baffled

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    4

    Program output is strange characters, totally baffled

    I had a search round the forum and found a post explaining it could be printing the pointer value. I am not so sure about that. I am using sqlite3.

    Why am I seeing these odd characters? I honestly cannot for the life of me figure it out.

    Many thanks.

    Output example:
    Code:
    chrono_search(): ��|�
    chrono_search() is where strange characters
    Code:
    void _concatenate_search_term()
    {
        search_term = (char *)malloc((list_count(_word_head) + 1) * sizeof(char));
    
    
        node_t * current = _word_head;
        while (current != NULL)
        {
            strncat(search_term, current->value, strlen(current->value));
            current = current->next;
        }
    }
    
    
    
    
    
    
    
    
    int chrono_check_word()
    {
        _concatenate_search_term();
    
    
        printf("query: %s\n", search_term);
    
    
        printf("restricted: %d\n", list_count(_restricted_words));
    
    
        node_t * current = _restricted_words;
        while (current != NULL)
        {
            if (strcmp(current->value, search_term) == 0)
            {
                // chrono_process_name();
                chrono_reset();
                printf("found!\n");
    
    
                chrono_socket_write("restricted");
                return RESTRICTED;
            }
    
    
            printf("chrono_search(): %s\n", current->value);
            current = current->next;
        }
    
    
        /* reset the search terms and word head. free() */
        chrono_reset();
    
    
        return ALLOWED;
    }
    DB
    Code:
    int select_callback(void * not_used, int argc, char * argv[], char ** col_name)
    {
        not_used = 0;
        size_t len = strlen(*argv)+1;
    
    
        const unsigned int restricted_col = 3;
    
    
        char * value = *argv;
    
    
        if (_restricted_words == NULL)
        {
            _restricted_words = list_create(value, NULL);
        }
        else
        {
            list_append(_restricted_words, value);
        }
    
    
        printf("%s\n", _restricted_words->value);
        printf("%s\n", value);
    
    
        return 0;
    }
    
    
    sqlite3 * db;
    char * err_msg;
    
    
    int rc = sqlite3_open("/usr/local/var/db/chronograff.db", &db);
    
    
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
    
    
        exit(EXIT_FAILURE);
    }
    
    
    const char * sql = "SELECT restricted FROM black_list WHERE constrained = 'command';";
    
    
    rc = sqlite3_exec(db, sql, select_callback, 0, &err_msg);
    
    
    if (rc != SQLITE_OK )
    {
    
    
        sqlite3_free(err_msg);
        sqlite3_close(db);
    
    
        exit(EXIT_FAILURE);
    }
    
    
    sqlite3_close(db);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > strncat(search_term, current->value, strlen(current->value));
    Malloc doesn't initialise the memory to contain zeros.
    So you append to an arbitrary amount of garbage - almost certainly resulting in a buffer overflow as a result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    4
    Quote Originally Posted by Salem View Post
    .. you append to an arbitrary amount of garbage - almost certainly resulting in a buffer overflow as a result.
    You seem to be right, however,

    Code:
    search_term = (char *)malloc((list_count(_word_head) + 1) * sizeof(char));
    memset(search_term, 0, sizeof(char));
    I have either been staring way too long or I am just not thinking straight.

    I still get these characters. Printing the value of before stncat tells me that it's fine before concatenating.

    Thanks salem.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How else are you abusing memory?

    Does list_count(_word_head) return what you think it should?

    Is the memory you have for search_term being overwritten because some other part of the program has a "use after free" bug?

    Are you using threads at all in this program? If you are, you just made debugging 1000's of times harder than it would otherwise be.


    BTW, calling memset just to set a single byte is dumb.
    search_term[0] = '\0';
    is all you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    4
    Quote Originally Posted by Salem View Post
    How else are you abusing memory?

    Does list_count(_word_head) return what you think it should?
    Yes

    Quote Originally Posted by Salem View Post
    Is the memory you have for search_term being overwritten because some other part of the program has a "use after free" bug?
    No

    Quote Originally Posted by Salem View Post
    Are you using threads at all in this program? If you are, you just made debugging 1000's of times harder than it would otherwise be.
    No

    Quote Originally Posted by Salem View Post
    BTW, calling memset just to set a single byte is dumb.
    search_term[0] = '\0';
    is all you need.
    Many thanks, I will do this now.

    Also I figured it out!

    We are looking at the wrong place. Thanks again btw, as soon as you jumped in I saw where I wasn't looking. Always the case. Ask for help, see the problem

    I am retrieving records from sqlite3 then setting values in a list using values from the sqlite result set. The callback is where the problem lies; I need to stncpy value into a new char outside the callback and then these characters are no more and the list behaves as expected.

    Happy days!

    Code:
    int select_callback(void * not_used, int argc, char * argv[], char ** col_name)
    {
        not_used = 0;
        const unsigned int restricted_col = 3;
    
    
        char * value = argv[restricted_col];
    
    
        if (_restricted_words == NULL)
        {
            _restricted_words = list_create(value, NULL);
        }
        else
        {
            list_append(_restricted_words, value);
        }
    
    
        printf("%s\n", _restricted_words->value);
    
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not sure why the output is strange characters
    By xants in forum C Programming
    Replies: 2
    Last Post: 07-14-2016, 12:20 AM
  2. Replies: 1
    Last Post: 03-16-2015, 10:36 AM
  3. Strange characters in output to terminal
    By hellogamesmaste in forum C Programming
    Replies: 4
    Last Post: 08-30-2009, 10:25 AM
  4. Strange characters in output Need Help ASAP
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2003, 06:35 PM
  5. Replies: 3
    Last Post: 02-19-2003, 08:34 PM

Tags for this Thread