字符串函数sprintf / sprintf_s的陷阱

其实,用C/C++做开发的童鞋,对sprintf不会陌生,对该函数的一些问题,一直想好好总结下:

如果第2个format(格式)参数中用%指定的后续参数个数与实际参数必须一致,否则可能会出各种问题。所谓一致是严格一致,看几个例子:

1. 如果你用的%d,对应的实参却用double,将得到错误的结果,而且更为严重的是,如果该参数后还有别的参数,将影响后面的参数解析:

char buf[1024];

double dV = 123.0;

int iLen = sprintf( buf, "Test:  %d, not one %d\n", dV, 1);

std::cout << buf;

并不会输出期望的结果:Test:  123, not one 1

我在VS2013下得到的输出是: Test:  0, not one 1079951360

如果我们知道函数调用和参数传递的机理,就会明白,sprintf 期望format参数后接2个int型参数(都是4字节的),但却给了个double参数和一个常数,我们知道double是8字节的,所以double就被分为了2个int,而后面的常数1压根就没用上,所以结果不可能正确。

结果要正确,必须将double参数强转成int:

int iLen = sprintf( buf, "Test:  %d, not one %d\n", (int)dV, 1);

2. format中指定的参数数目与实际的数目不一致时,也得不到正确的结果:

int iLen = sprintf( buf, "Test:  %d, not one %d\n", 1);    // 实参少1个

VS2013下得到的输出:Test:  1, not one -84618255

其实后面显示的数是随机的,就是函数调用栈实参1后面的一个4字节数,这个例子不会导致宕机,因为第2个实参取的是栈上的随机数(未初始化数)。


3. 如果format参数中有%s,则对应的实参一定得给对了,否则极其可能宕机:

char buf[1024];

double dV = 123.0;

int iLen = sprintf( buf, "Test:  %s, not one %d\n", dV, 1);

std::cout << buf;

在Debug模式下,将断点设置在最后一行,将会发现调用栈完全不对了,因为sprintf将dV的值当字符串地址看待,所以宕掉

char buf[1024];

std::string str = "hello";

int iLen = sprintf( buf, "Test:  %s, not one %d\n", str, 1);

std::cout << buf;

这个也不会输出正常结果, 会把str对象(copy的对象)的空间当作字符串地址

VS2013下得到的输出:  Test:  (null), not one -858993460 (还算幸运,没宕机)

sprintf不会帮你做字符串转换,你的主动告诉它。


最后,第1个缓冲区参数,需要有足够的空间,否则很容易越界导致各种问题。

sprintf_s在缓冲区后加了个缓冲区大小参数,VS的实现会将缓冲区按缓冲区大小参数memset,所以,如果缓冲区大小参数不正确,又将可能导致新的问题--越界


#include "cJSON.h" const char *voice_cmd[] = {"场景模式", "回家模式", "离家模式", "会客模式", "阅读模式", "睡眠模式", "娱乐模式", "温馨模式", "明亮模式", "观影模式","灯光亮一点", "灯光最亮", "灯光暗一点", "灯光最暗","灯光暖一点","灯光最暖", "灯光冷一点", "灯光最冷", "打开窗帘", "窗帘半开", "窗帘停", "打开空调", "空调制热", "空调制冷", "关闭空调", "打开风扇", "关闭风扇", "开灯", "关灯", "关闭窗帘"}; /* mqtt JSON数据发送集中处理函数 */ void mqtt_json_send(int type_temp) { cJSON *pRoot = cJSON_CreateObject(); // 创建JSON根部结构体 cJSON *pValue = cJSON_CreateObject(); // 创建JSON子叶结构体 cJSON *pValue2 = cJSON_CreateObject(); // 创建JSON子叶结构体 cJSON *userid = cJSON_CreateObject(); // 创建JSON子叶结构体 // Preparing_encrypted_msg((uint8_t *)json_data, strlen(json_data), 1); switch (type_temp) { case 0: // 上报睡眠带数据 { cJSON_AddStringToObject(pRoot, "action", "upload_sleep_data"); cJSON_AddItemToObject(pRoot, "device", pValue); cJSON_AddStringToObject(pValue, "mac", STA_MAC_string_hex); cJSON_AddStringToObject(pValue, "type", "80"); cJSON_AddItemToObject(pRoot, "data", pValue2); cJSON_AddStringToObject(pValue2, "type", device_sleep.type_name); cJSON_AddStringToObject(pValue2, "Odata", (char *)device_sleep.Odata); cJSON_AddNumberToObject(pValue2, "index", device_sleep.index); cJSON_AddNumberToObject(pValue2, "status", device_sleep.status); cJSON_AddNumberToObject(pValue2, "heart_rate", device_sleep.heart_rate); cJSON_AddNumberToObject(pValue2, "breathe_rate", device_sleep.breathe_rate); } break; case 1: // SOS 悉知 { cJSON_AddStringToObject(pRoot, "action", "know_dev_alert"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddStringToObject(pValue, "alert_mac", sos_other_mac); } break; case 2: // SOS 悉知 { cJSON_AddStringToObject(pRoot, "action", "handle_dev_alert"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddStringToObject(pValue, "alert_mac", sos_other_mac); } break; case 3: // 发送设备数据 { int battery_voltage = 0; xQueuePeek(adc_battery_queue, &battery_voltage, 0); // 因为关闭了光感功能,所以不再读取光感电压 //int light_voltage = 0; //xQueuePeek(adc_light_queue, &light_voltage, 0); cJSON_AddStringToObject(pRoot, "action", "debug_msg"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddNumberToObject(pValue, "battery_voltage", battery_voltage); //cJSON_AddNumberToObject(pValue, "light_voltage", light_voltage); cJSON_AddNumberToObject(pValue, "sos_flag", sos.sos_flag); cJSON_AddNumberToObject(pValue, "sos_mode", sos.mode); cJSON_AddNumberToObject(pValue, "iram_free_size", iram_free_size); #if DEFINE_HARDWARE_NB cJSON_AddNumberToObject(pValue, "nb_rssi", my_nb.rssi - 256); #endif } break; case 4: // 发送SOS触发方式 { cJSON_AddStringToObject(pRoot, "action", "debug_msg"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddNumberToObject(pValue, "type", sos.type); cJSON_AddNumberToObject(pValue, "dev", sos.device_type); } break; case 5: // 发送SOS解除方式 { cJSON_AddStringToObject(pRoot, "action", "debug_msg"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddNumberToObject(pValue, "type", sos.dis_type); cJSON_AddNumberToObject(pValue, "dev", sos.device_type); } break; case 6: // 上传本地日志 { int address = 0; address = my_log_read(); if (address == -1) { cJSON_AddStringToObject(pRoot, "action", "log_msg"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddStringToObject(pValue, "local_log", "get fail"); my_log_new(); } else { queue_data_t *data = (queue_data_t *)address; ESP_LOGW(TAG, "len = %d, data : %s", data->len, data->data); cJSON_AddStringToObject(pRoot, "action", "log_msg"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddStringToObject(pValue, "local_log", (char *)data->data); free((char *)address); my_log_new(); } } break; case 7: // 发送电池电压 { int battery_voltage = 0; xQueuePeek(adc_battery_queue, &battery_voltage, 0); cJSON_AddStringToObject(pRoot, "action", "report_msg"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddNumberToObject(pValue, "battery_voltage", battery_voltage); cJSON_AddStringToObject(pValue, "battery_status", "dry"); } break; case 8: // 上报WIFI信息 { cJSON_AddStringToObject(pRoot, "action", "report_wifi_ap_msg"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddStringToObject(pValue, "s1", (char *)backup_wifi_config[0].ssid); cJSON_AddStringToObject(pValue, "p1", (char *)backup_wifi_config[0].password); cJSON_AddStringToObject(pValue, "s2", (char *)backup_wifi_config[1].ssid); cJSON_AddStringToObject(pValue, "p2", (char *)backup_wifi_config[1].password); } break; case 9: // 上报睡眠带分钟数据 { cJSON_AddStringToObject(pRoot, "action", "upload_sleep_data_min"); cJSON_AddItemToObject(pRoot, "device", pValue); cJSON_AddStringToObject(pValue, "mac", STA_MAC_string_hex); cJSON_AddStringToObject(pValue, "type", "80"); cJSON_AddItemToObject(pRoot, "data", pValue2); cJSON_AddStringToObject(pValue2, "type", device_sleep.type_name); // cJSON_AddStringToObject(pValue2, "Odata", (char *)device_sleep.Odata); cJSON_AddNumberToObject(pValue2, "index", device_sleep.index_min); cJSON_AddNumberToObject(pValue2, "status", device_sleep.status_min); cJSON_AddNumberToObject(pValue2, "heart_rate", device_sleep.heart_rate_min); cJSON_AddNumberToObject(pValue2, "breathe_rate", device_sleep.breathe_rate_min); cJSON_AddNumberToObject(pValue2, "body_move_cnt", device_sleep.body_move_cnt); cJSON_AddNumberToObject(pValue2, "snore_cnt", device_sleep.snore_cnt); cJSON_AddNumberToObject(pValue2, "breathe_rate", device_sleep.breathe_rate_min); cJSON_AddNumberToObject(pValue2, "respiratory_disturbance", device_sleep.respiratory_disturbance); cJSON_AddNumberToObject(pValue2, "PTHD", device_sleep.PTHD); cJSON_AddNumberToObject(pValue2, "temperature", device_sleep.temperature); } break; case 10: // 回复OK { cJSON_AddStringToObject(pRoot, "action", action_type); cJSON_AddStringToObject(pRoot, "data", "OK"); } break; case 11: // 触发求助 { char str_type[8] = {0}; cJSON_AddStringToObject(pRoot, "action", "sos_start"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddNumberToObject(pValue, "type", sos.type); sprintf(str_type, "%02X", sos.device_type); cJSON_AddStringToObject(pValue, "dev", str_type); } break; case 12: // 解除求助 { char str_type[8] = {0}; cJSON_AddStringToObject(pRoot, "action", "sos_stop"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddNumberToObject(pValue, "type", sos.dis_type); sprintf(str_type, "%02X", sos.device_type); cJSON_AddStringToObject(pValue, "dev", str_type); } break; case 13: // 设备回应邀请 { cJSON_AddStringToObject(pRoot, "action", "device_join_rsp"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddNumberToObject(pValue, "type", 1); } break; case 14: // 重复发送报警 { char str_type[8] = {0}; cJSON_AddStringToObject(pRoot, "action", "sos_resend"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddNumberToObject(pValue, "type", sos.type); sprintf(str_type, "%02x", sos.device_type); cJSON_AddStringToObject(pValue, "dev", str_type); } break; // case 14: // 请求不通过 // { // cJSON_AddStringToObject(pRoot, "action", "add_ack"); // cJSON_AddItemToObject(pRoot, "data", pValue); // cJSON_AddStringToObject(pValue, "result", "ERR"); // } // break; case 15: { cJSON_AddStringToObject(pRoot, "action", "upload_all_sleep_report"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddStringToObject(pValue, "report", (char *)device_sleep.report_data); } break; case 16: { cJSON_AddStringToObject(pRoot, "action", "upload_sleep_report_detail"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddNumberToObject(pValue, "time", device_sleep.report_time); cJSON_AddStringToObject(pValue, "detail", (char *)device_sleep.report_data); } break; case 17: // 自学习成功 { cJSON_AddStringToObject(pRoot, "action", "learn_help_ack"); cJSON_AddStringToObject(pRoot, "data", "success"); } break; case 18: // 自学习失败或退出 { cJSON_AddStringToObject(pRoot, "action", "learn_help_ack"); cJSON_AddStringToObject(pRoot, "data", "fail"); } break; case 19: { uint8_t temp_data_cmd = 0; xQueuePeek(offline_voice_cmd_queue, (void *)&temp_data_cmd, 0); cJSON_AddStringToObject(pRoot, "action", "voice_cmd"); cJSON_AddStringToObject(pRoot, "data", voice_cmd[temp_data_cmd]); } break; case 20: //设备回应邀请(设备->云端) { cJSON_AddStringToObject(pRoot, "action", "device_join_rsp"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddNumberToObject(pValue, "type", 2); } break; case 21: //SOS识别到上报 { cJSON_AddStringToObject(pRoot, "action", "sos_detected"); } break; case 22: //产测完成上报 { cJSON_AddStringToObject(pRoot, "action", "testing_completed"); } break; case 27: { cJSON_AddStringToObject(pRoot, "action", "confirm_add_device_rsp"); cJSON_AddItemToObject(pRoot, "data", userid); cJSON_AddNumberToObject(userid, "userid", g_userid); } break; case 28: { cJSON_AddStringToObject(pRoot, "action", "wake_up"); } break; case 29: { int tim_show; tim_show = update_cnt; cJSON_AddStringToObject(pRoot, "action", "time_new"); cJSON_AddItemToObject(pRoot, "data", pValue); cJSON_AddNumberToObject(pValue, "type", tim_show); } break; case 30: { cJSON_AddStringToObject(pRoot, "action", "LCD_Config"); } break; /*case 31: { //uint8_t data_cmd_id = 0; xQueuePeek(offline_voice_cmd_queue, (void *)&data_cmd_id, 0); cJSON_AddStringToObject(pRoot, "action", "new_voice_cmd"); cJSON_AddStringToObject(pRoot, "data", voice_cmd[data_cmd_id]); }*/ /*break; case 32: { cJSON_AddStringToObject(pRoot, "action", "new_voice_cmd"); cJSON_AddStringToObject(pRoot, "data", voice_cmd[0x10]); } break;*/ case 51: { #if DEFINE_SOFT_blood_pressure extern uint8_t blood_pressure_result[6]; char temp_data[16] = {0}; sprintf(temp_data, "%02x%02x%02x%02x%02x%02x", blood_pressure_result[0], blood_pressure_result[1], blood_pressure_result[2], blood_pressure_result[3], blood_pressure_result[4], blood_pressure_result[5]); cJSON_AddStringToObject(pRoot, "action", "blood_pressure_result"); cJSON_AddStringToObject(pRoot, "data", temp_data); #endif } break; case 0xFF: { } break; default: break; } //printf("esp_get_minimum_free_heap_size : %d \n", esp_get_minimum_free_heap_size()); //printf("[APP] Free memory: %d bytes\n", esp_get_free_heap_size()); char *json_data = cJSON_PrintUnformatted(pRoot); // ESP_LOGI(TAG, "json_data : \n%s\n", json_data); /*if (!json_data) { cJSON_Delete(pRoot); return; }*/ int json_data_len = strlen(json_data); // ESP_LOGI(TAG, "json_data_len : %d", json_data_len); Preparing_encrypted_msg((uint8_t *)json_data, json_data_len, 1); cJSON_free(json_data); cJSON_Delete(pRoot); ESP_LOGE("MEM", "[msgrecv] Free heap: %u", (unsigned int)esp_get_free_heap_size()); } 这是我的json发送函数,请您看看是哪里会内存泄露
最新发布
08-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值