Segmentation fault slider event

Hi Team,


Getting Segmentation Fault when i pass the slider value to function.
when i use the slider getting segmentation fault at mqtt_publish(buf); function.
How to pass this char buf[4] value to my function?

static void slider_event_cb(lv_obj_t * slider, lv_event_t event)
    if(event == LV_EVENT_RELEASED) {
        static char buf[4]; /* max 3 bytes for number plus 1 null terminating byte */
        snprintf(buf, 4, "%u", lv_slider_get_value(slider));
        lv_label_set_text(slider_label, buf);

        }else if(lv_slider_get_value(slider)>0){
        lv_label_set_text(wattage,strcat(buf," W"));
        mqtt_publish(buf);          //segmentation fault
static void mqtt_publish(char *message){

   MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
   MQTTClient_message pubmsg = MQTTClient_message_initializer;
   MQTTClient_deliveryToken token;
   int rc;

   MQTTClient_create(&client, ADDRESS, CLIENTID,
   conn_opts.keepAliveInterval = 30;
   conn_opts.cleansession = 1;
   conn_opts.username= "username";
   conn_opts.password= "password";
   if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
       printf("Failed to connect, return code %d\n", rc);
   pubmsg.payload = message;
   pubmsg.payloadlen = (int)strlen(message);
   pubmsg.qos = QOS;
   pubmsg.retained = 0;
   MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
   rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
   MQTTClient_disconnect(client, 10000);


Can you please help me with this.
Thanks in Advance.

The number can have up to three characters, and there’s one byte left for null termination. However, you are trying to strcat two additional characters after the number, meaning that you really need buf to be 6 bytes long (3 for the number, two for a space and W, and one for the null terminator).

1 Like

i am not sending strcat value to mqtt_publish(buf); function. sending slider value(buf) from 0 to 100 only
i am showing strcat value to label.

Did you try changing buf to a length of 6?

Issue resolved thanks