0

As a student with a background in Java, C#, a bit of Python and web-based languages, I am currently learning C. For practice I am coding a GTK application to convert a longer text to smaller pieces of text of 140 characters max. This use case is meant to post longer texts on Twitter easily.

So I experimented a bit and the application is working more or less at this moment. However, it regularly shows unexpected behaviour. When inserting text in the GTK interface (TextView) and clicking a button calling the below method, the output put back into the TextView sometimes looks like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut eni (1/4)

m ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in rep (2/4)

(0/1981836649)

rehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in (3/4)

(0/1763734633)

(1701998412/544044403)

culpa qui officia deserunt mollit anim id est laborum. (4/4)

Or similar when inserting Lorem Ipsum as input. Sometimes it also cuts an entire part (e.g., only the (4/4) part is printed). The expected output is:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut eni (1/4)

m ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip > ex ea commodo consequat. Duis aute irure dolor in rep (2/4)

rehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in (3/4)

culpa qui officia deserunt mollit anim id est laborum. (4/4)

It applies to the following method, which is hooked to a GTK button:

void on_do_tweet_clicked()
{    
    char *text = get_text_from_text_view(g_tv_tweet_text);
    int text_length = strlen(text);
    int tweet_no = text_length / 130 + (text_length % 130 != 0);
    char tweets[(int) tweet_no][140 * sizeof(char*)];

    for (int i = 0; i < tweet_no; i++)
    {
        char *tweet = (char*) malloc(sizeof(char*) * 140); 
        if (tweet_no != 1) {
            if (i + 1 == tweet_no) {
                strcpy(tweet, &text[i * 130]);
            } else {
                strncpy(tweet, &text[i * 130], 130);
            }
            strcat(tweet, " (%d/%i)\n\0");
            sprintf(tweets[i], tweet, i + 1, tweet_no);
        } else {
            strcpy(tweet, &text[i * 130]);
            sprintf(tweets[i], tweet);
        }

        free(tweet); 
    }

    free(text);

    GtkTextBuffer *buffer = gtk_text_view_get_buffer((GtkTextView *) g_tv_tweet_text);
    gtk_text_buffer_set_text(buffer, "", 0);

    for (int i = 0; i < sizeof(tweets) / sizeof(tweets[0]); i++)
    {
        gtk_text_buffer_insert_at_cursor(buffer, tweets[i], strlen(tweets[i]));
    }

    memset(tweets, 0, sizeof(tweets));
} 

I did some research on the problem and I suspect it has to do with either an problem with encodings or a problem related to pointers. However, my searches on the web did not result in a solution and my knowledge of C is still too limited to draw my own conclusions.

So here's the question: could anyone explain why this method is showing this behaviour and give me a hint on how to solve it?

Thank you in advance!

10
  • 3
    char *tweet = (char*) malloc(sizeof(char*) * 140); - why is sizeof(char*) used here? Jul 2, 2017 at 20:58
  • 3
    Function strncpy is not intended to be used with C strings. In general case, it does not zero terminate the target buffer. In your code I see strncpy folowed by strcat - this is exactly the case when strncpy's failure to trerminate the buffer will result in nonsense. Jul 2, 2017 at 21:00
  • I love valgrind. I'll bet you will too if you make friends with it.
    – Jeff Holt
    Jul 2, 2017 at 21:01
  • @AnT (1) I thought maybe this was a better practice as I am allocating based on the environment (32 or 64-bit)
    – Luc
    Jul 2, 2017 at 21:03
  • sprintf(tweets[i], tweet) - this looks like a dangerous equivalent of strcpy. Why would you do it, if you already knew about strcpy? What exactly did you attempt to achieve with that sprintf? Jul 2, 2017 at 21:04

1 Answer 1

0

Thanks to @AnT I got it working. The final code is:

void on_do_tweet_clicked()
{    
    char *text = get_text_from_text_view(g_tv_tweet_text);
    int text_length = strlen(text);
    int tweet_no = text_length / 130 + (text_length % 130 != 0);
    char tweets[(int) tweet_no][140 * sizeof(char)];

    for (int i = 0; i < tweet_no; i++)
    {
        char *tweet = (char*) malloc(sizeof(char) * 140); 
        if (tweet_no != 1) {
            if (i + 1 == tweet_no) {
                strcpy(tweet, &text[i * 130]);
            } else {
                strncpy(tweet, &text[i * 130], 130);
            }
            tweet[130] = '\0';
            strcat(tweet, " (%d/%i)\n");
            sprintf(tweets[i], tweet, i + 1, tweet_no);
        } else {
            strcpy(tweets[i], &text[i * 130]);
        }

        free(tweet); 
    }

    free(text);

    GtkTextBuffer *buffer = gtk_text_view_get_buffer((GtkTextView *) g_tv_tweet_text);
    gtk_text_buffer_set_text(buffer, "", 0);

    for (int i = 0; i < sizeof(tweets) / sizeof(tweets[0]); i++)
    {
        gtk_text_buffer_insert_at_cursor(buffer, tweets[i], strlen(tweets[i]));
    }

    memset(tweets, 0, sizeof(tweets));
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.