If you do not use sprintf then everything works fine.

 int some_f() { SEGGER_RTT_TerminalOut(2, (char*)modem_data); uint8_t amount = 0; char sep[4] = "\r\n"; char *array[32]; char *pstr; pstr = strtok((char*)modem_data, sep); //разделяем несколько сообщений друг от друга while(pstr != NULL) { array[amount] = pstr; amount++; pstr = strtok(NULL, sep); } amount--; while(amount > 0) { uint8_t sms_index = 0; sms_index = (uint8_t)hex_char(array[amount-2][7]); if(strstr(array[amount-2], "REC UNREAD") > 0) //читаем только непрочитанные { //char _str[64]; char _sep[5] = "\",\""; char *_array[32]; char *_pstr; uint8_t _amount = 0; char sms_string[125]; memset(sms_string, 0, sizeof(sms_string)); _pstr = strtok(array[amount-2], _sep); while(_pstr != NULL) //разделяем сообщение { _array[_amount] = _pstr; _amount++; _pstr = strtok(NULL, _sep); } _amount--; SEGGER_RTT_printf(0, "amount = %d\r\n", amount); SEGGER_RTT_printf(0, "sms_index = %d\r\n", sms_index); SEGGER_RTT_printf(0, "%s\r\n", array[amount-2]); while(_amount) { SEGGER_RTT_printf(0, "%s\r\n", _array[_amount]); _amount--; } SEGGER_RTT_printf(0, "%s\r\n", array[amount-1]); //_array[2] номер //_array[3] время //_array[4] дата //array[amount-1] текст sprintf(sms_string,"%s %s", _array[2], array[amount-1]); mqtt_put("sms", sms_string); memset(sms_string, 0, sizeof(sms_string)); memset(_array, 0, sizeof(_array)); } amount-=2; } memset(array, 0, sizeof(array)); at_write("+CMGDA=\"DEL READ\""); //удаляем прочитанные return 0; } void mqtt_put(char *topic_name_p, char *content_p) { uint32_t err_code; char *content = (char*)malloc((strlen(content_p)+1) * sizeof(char)); //выделяем память под content char *topic_name = (char*)malloc((strlen(topic_name_p)+1)* sizeof(char)); //выделяем память под topic_name strlcpy(content, content_p, strlen(content_p)+1); //копируем в новое место content strlcpy(topic_name, topic_name_p, strlen(topic_name_p)+1);//копируем в новое место topic_name mqtt_fifo_t *mqtt_fifo = malloc(sizeof(mqtt_fifo_t)); //выделяем память под структуру mqtt_fifo->content = content; mqtt_fifo->topic_name = topic_name; //помещяем в структуру ссылки на строки if(mqtt_fifo != NULL && mqtt_fifo->content !=NULL && mqtt_fifo->topic_name !=NULL) //проверям на было ли ошибок при выделении памяти { //здесь код который выполняется если всё нормально } else { SEGGER_RTT_SetTerminal(1); SEGGER_RTT_printf(0, "MALLOC ERR"); //Эта функция просто выводит сообщение об ошибке SEGGER_RTT_SetTerminal(0); } } 

Before calling some_f , mqtt_put works without any complaints, but after, malloc returns a null pointer. This is only one of the examples of using sprintf and the most irrational, but further uses are other options that also break malloc . Further along, all allocated memory is cleared and released.

  • Moment with sprintf(str, "%s%s", str ,s); I fixed it but it did not affect anything. - Nael Agliev
  • Show what exactly you fixed. Now written nonsense, which does not even compile. - AnT
  • It is clear that this code simply does not compile, but I would like to say that even in compiling code (you will probably achieve this eventually), before calling strlen() you need to be sure that the data in the buffer ends in zero (you have the same no hint of buffer initialization) - avp
  • Okay. Here is a piece of working code that compiles everything with it well and that breaks if you use sprintf in it - Nael Agliev
  • @AnT thanks fixed. But it still does not solve anything - Nael Agliev

0