Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8250457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:53:45+00:00 2026-06-07T23:53:45+00:00

I am getting segfault on this function. /** * Excutes the passed query and

  • 0

I am getting segfault on this function.

/**
 * Excutes the passed query and returs the the first row as an array of 
 * strings. You must free this array by calling g_strfreev()
 */
static gchar** mysql_single_row(MYSQL *mysql_handle, char* query){
    my_ulonglong num_rows=0;
    MYSQL_RES *result = NULL;
    gchar ** single_row = NULL;
    GString *q = g_string_new(query);
    MYSQL_ROW row={0};
    int query_status = mysql_real_query(mysql_handle, q->str, q->len);

    if(query_status!=0){
        g_string_free(q, TRUE);
        return NULL;
    }
    fprintf(stderr, "Storing mysql result!\n");
    result = mysql_store_result(mysql_handle);

    if(result==NULL){
            /// it was not a query that returns statemnet (e.g. INSERT, DELETE)
            g_string_free(q, TRUE);
            return NULL;
    }

    num_rows = mysql_num_rows(result);

    fprintf(stderr, "Total rows = %Ld\n", num_rows);

    if(num_rows>0){
            /// We only fetch the first row
            row = mysql_fetch_row(result);
            fprintf(stderr, "Copy single rows\n");
            single_row =  g_strdupv(row); // <------------- SIGSEGV happens here
            fprintf(stderr, "Copy single rows done\n");
    }else{
            mysql_free_result(result);
            g_string_free(q, TRUE);
            return NULL;
    }

    /// clean up
    g_string_free(q, TRUE);
    mysql_free_result(result);
    return single_row;
}

basically what I want to do is to execute some ‘SELECT’ query and return the first row as an array of strings. According to the manual g_strdupv should copy the returned char ** and makes a new one. I return this. Later I clean this up using g_strfreev which is the recommended method.

But why I am getting segfaults here. I ran it with valgrind. Output and the corresponding code can be found here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:53:46+00:00Added an answer on June 7, 2026 at 11:53 pm

    g_strdupv() copies a NULL-terminated array of C strings (which each must be NUL-terminated). The MySQL documentation on the C API Data Structures states that MYSQL_ROW is an array of byte strings which are not necessarily NUL-terminated “if field values may contain binary data”. Thus a MYSQL_ROW is neither guaranteed to be a NULL-terminated array nor an array of C strings.

    The segfault is likely occurring because g_strdupv() keeps looking for the NULL terminator, but doesn’t find one until it attempts a read of non-process memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting segfaults when calling a dbus method inside a thread. This is my
I'm getting a segfault that GDB says is coming from SDL_GL_SwapBuffers. However, I can't
I am implementing a function to recursively reverse a linked-list, but getting seg-fault. typedef
Possible Duplicate: casting char[][] to char** causes segfault? I have a 2D array declared
I wrote this function to join two paths in C; void *xmalloc(size_t size) {
this is my first time posting a question here - I've searched for ones
This is quite strange for me, but I'm getting an unexpected and random segmentation
I am trying to print out an array of integers. I am getting a
Getting the subdomain from a URL sounds easy at first. http://www.domain.example Scan for the
so I am getting a segfault from ctypes on a 32 bit linux machine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.