Wrapper around C API is crashing

Crank Storyboard Designer samples and code snippets.

Moderator: jamiev

Wrapper around C API is crashing

Postby hisham.veeran » Tue May 08, 2018 7:50 pm

I took the sample code and added a wrapper around it. The following code works on it's first execution, crashes due to memory corruption after that.

Code: Select all
int sendEvent(const char *  event_name,
             const char *  event_format,
             const cluster_update_event_t* event_data){

   gre_io_t                    *send_handle;
    gre_io_serialized_data_t    *nbuffer = NULL;
   int                   ret;

    // Connect to a channel to send messages (write)
   send_handle = gre_io_open("cluster", GRE_IO_TYPE_WRONLY);
    if (send_handle == NULL) {
        fprintf(stderr, "Can't open send channel\n");
        return 1;
   }

   // Serialize the data to a buffer
   nbuffer = gre_io_serialize(nbuffer, NULL, event_name, event_format, event_data, sizeof(*event_data));
   if (!nbuffer) {
       fprintf(stderr, "Can't serialized data to buffer, exiting\n");
      return 1;
   }

   // Send the serialized event buffer
   ret = gre_io_send(send_handle, nbuffer);
   if (ret < 0) {
      fprintf(stderr, "Send failed, exiting\n");
      return 1;
   }

   //Release the buffer memory, close the send handle
   gre_io_free_buffer(nbuffer);
   gre_io_close(send_handle);

   return 0;
}


Log:
Code: Select all
audit: type=1701 audit(1525306930.200:5): auid=4294967295 uid=0 gid=0 ses=4294967295 pid=371 comm=“cluster_update” exe=“/cluster_update” sig=6
*** Error in `/cluster_update’: malloc(): memory corruption (fast): 0x00f0e020 ***
Aborted (core dumped)


Any help to find the memory corruption is appreciated.
hisham.veeran
 
Posts: 3
Joined: Tue May 08, 2018 4:28 pm

Re: Wrapper around C API is crashing

Postby rdowdall » Tue May 08, 2018 8:03 pm

Hello

Does the cluster sample work without modification? What you have done below looks to be okay.

Can you share the complete source that you have? It's possible the corruption is occurring in another place in the code.

Thanks,
Rodney
rdowdall
 
Posts: 104
Joined: Mon Nov 02, 2009 9:46 pm

Re: Wrapper around C API is crashing

Postby hisham.veeran » Tue May 08, 2018 8:30 pm

Hi Rodeny,

It doesn't crash if I comment out gre_io_* calls. Few relevant code are as follows:

Code: Select all
void compareBuffer(char buffer[], int size){
   printf("size : %d, buffer %s", size, buffer);
   
   char *pData = malloc(size);
   strcpy(pData, buffer);

   char *array[4];
   const char delim[2] = "|";
   int i=0;
   array[i] = strtok(pData, delim);
   while(array[i]!=NULL)
   {
      array[++i] = strtok(NULL, delim);
   }

   if(strncmp(array[0], "caution_door", size) == 0){
      openClusterDoor();
   }else if(strncmp(array[0], "media_show", size) == 0){
      showClusterMedia();
   }else if(strncmp(array[0], "caution_trunk", size) == 0){
      openClusterTrunk();
   }else if(strncmp(array[0], "animateGauge", strlen(array[0])) == 0){
      changeSpeed(atoi(array[1]), atoi(array[2]), atoi(array[3]));
   }else if(strncmp(array[0], "turn_off_server", size) == 0){
      isStop=true;
   }
   free(pData);
}


Code: Select all
int openClusterDoor(void){
   printf("Opening Door\n");
   return sendEvent(CLUSTER_DOOR_OPEN_EVENT, NULL, NULL);
}

int openClusterTrunk(void){
   printf("Opening Trunk\n");
   return sendEvent(CLUSTER_OPEN_TRUNK_EVENT, NULL, NULL);
}

int showClusterMedia(void){
   printf("Show media\n");
   return sendEvent(CLUSTER_SHOW_MEDIA_EVENT, NULL, NULL);
}

int setGauges(const int speed, const int rpm) {
    printf("Change the speed to %d and rpm to %d.\n", speed, rpm);
    cluster_update_event_t       event_data;
    memset(&event_data, 0, sizeof(event_data));
    event_data.speed = speed;
   event_data.rpm = rpm;

    return sendEvent(CLUSTER_UPDATE_GAUGES_EVENT, CLUSTER_UPDATE_GAUGES_FMT, &event_data);
}
hisham.veeran
 
Posts: 3
Joined: Tue May 08, 2018 4:28 pm

Re: Wrapper around C API is crashing

Postby rdowdall » Tue May 08, 2018 8:46 pm

Hello

I think the problem is with this line:

nbuffer = gre_io_serialize(nbuffer, NULL, event_name, event_format, event_data, sizeof(*event_data));

The calls for sendEvent are passing in NULL for event data. I believe that sizeof(NULL) is undefined, and in some cases can return 4, which is not the amount of data that you have there.

I would suggest adding in a fourth parameter to your function, which is data_size. Replace sizeof(*event_data) with data_size, and when you are calling the sendEvent function without any data, pass in a data_size of zero.

Is the function compareBuffer being called multiple times when the crash occurs?

Another thing to check is to make sure that the size that is being passed in to compareBuffer includes the NULL terminating string, because if it doesn't, you could be running off the end of pData when you are doing the strcpy.

Thanks,
Rodney
rdowdall
 
Posts: 104
Joined: Mon Nov 02, 2009 9:46 pm

Re: Wrapper around C API is crashing

Postby hisham.veeran » Wed May 09, 2018 10:08 am

Fixed the bugs. Thanks for your help.
hisham.veeran
 
Posts: 3
Joined: Tue May 08, 2018 4:28 pm


Return to Samples

Who is online

Users browsing this forum: No registered users and 0 guests

cron