Skip to content

Commit 4e963ad

Browse files
authored
Merge pull request #459 from Master92/compile_warnings
Prevent possible invalid memory accesses by sprintf
2 parents ed01b32 + 26e1d41 commit 4e963ad

33 files changed

Lines changed: 245 additions & 245 deletions

src/core/dvr.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ void dvr_update_status() {
4444
void dvr_enable_line_out(bool enable) {
4545
char buf[128];
4646
if (enable) {
47-
sprintf(buf, "%s out_on", AUDIO_SEL_SH);
47+
snprintf(buf, sizeof(buf), "%s out_on", AUDIO_SEL_SH);
4848
system_exec(buf);
49-
sprintf(buf, "%s out_linein_on", AUDIO_SEL_SH);
49+
snprintf(buf, sizeof(buf), "%s out_linein_on", AUDIO_SEL_SH);
5050
system_exec(buf);
51-
sprintf(buf, "%s out_dac_off", AUDIO_SEL_SH);
51+
snprintf(buf, sizeof(buf), "%s out_dac_off", AUDIO_SEL_SH);
5252
system_exec(buf);
5353
} else {
54-
sprintf(buf, "%s out_off", AUDIO_SEL_SH);
54+
snprintf(buf, sizeof(buf), "%s out_off", AUDIO_SEL_SH);
5555
system_exec(buf);
5656
}
5757
}
@@ -65,7 +65,7 @@ void dvr_select_audio_source(uint8_t source) {
6565

6666
if (source > 2)
6767
source = 2;
68-
sprintf(buf, "%s %s", AUDIO_SEL_SH, audio_source[source]);
68+
snprintf(buf, sizeof(buf), "%s %s", AUDIO_SEL_SH, audio_source[source]);
6969
system_exec(buf);
7070
}
7171

src/core/osd.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void osd_resource_path(char *buf, const char *fmt, osd_resource_t osd_resource_t
7070

7171
va_list args;
7272
va_start(args, osd_resource_type);
73-
vsprintf(filename, fmt, args);
73+
vsnprintf(filename, sizeof(filename), fmt, args);
7474
va_end(args);
7575
strcpy(buf2, buf);
7676
strcpy(buf, RESOURCE_PATH_SDCARD);
@@ -292,12 +292,12 @@ void osd_channel_show(bool bShow) {
292292
if (channel_osd_mode & 0x80) {
293293
ch = channel_osd_mode & 0x7F;
294294
color = lv_color_make(0xFF, 0x20, 0x20);
295-
sprintf(buf, " To %s? ", channel2str(g_setting.source.hdzero_band, ch));
295+
snprintf(buf, sizeof(buf), " To %s? ", channel2str(g_setting.source.hdzero_band, ch));
296296
lv_obj_set_style_bg_opa(g_osd_hdzero.channel[is_fhd], LV_OPA_100, 0);
297297
} else {
298298
ch = g_setting.scan.channel & 0x7F;
299299
color = lv_color_make(0xFF, 0xFF, 0xFF);
300-
sprintf(buf, "CH:%s", channel2str(g_setting.source.hdzero_band, ch));
300+
snprintf(buf, sizeof(buf), "CH:%s", channel2str(g_setting.source.hdzero_band, ch));
301301
lv_obj_set_style_bg_opa(g_osd_hdzero.channel[is_fhd], 0, 0);
302302
}
303303

@@ -644,13 +644,13 @@ void osd_hdzero_update(void) {
644644
lv_obj_add_flag(g_osd_hdzero.ant3[is_fhd], LV_OBJ_FLAG_HIDDEN);
645645

646646
if (g_setting.storage.selftest) {
647-
sprintf(buf, "T:%d-%d", fan_speed.top, g_temperature.top / 10);
647+
snprintf(buf, sizeof(buf), "T:%d-%d", fan_speed.top, g_temperature.top / 10);
648648
lv_label_set_text(g_osd_hdzero.osd_tempe[is_fhd][0], buf);
649649

650-
sprintf(buf, "L:%d-%d", fan_speed.left, g_temperature.left / 10);
650+
snprintf(buf, sizeof(buf), "L:%d-%d", fan_speed.left, g_temperature.left / 10);
651651
lv_label_set_text(g_osd_hdzero.osd_tempe[is_fhd][1], buf);
652652

653-
sprintf(buf, "R:%d-%d", fan_speed.right, g_temperature.right / 10);
653+
snprintf(buf, sizeof(buf), "R:%d-%d", fan_speed.right, g_temperature.right / 10);
654654
lv_label_set_text(g_osd_hdzero.osd_tempe[is_fhd][2], buf);
655655
}
656656
}
@@ -929,13 +929,13 @@ void load_fc_osd_font(uint8_t fhd) {
929929
int i;
930930

931931
if (fhd) {
932-
sprintf(fp[0], "%s%s_FHD_000.bmp", FC_OSD_SDCARD_PATH, fc_variant);
933-
sprintf(fp[1], "%s%s_FHD_000.bmp", FC_OSD_LOCAL_PATH, fc_variant);
934-
sprintf(fp[2], "%sBTFL_FHD_000.bmp", FC_OSD_LOCAL_PATH);
932+
snprintf(fp[0], sizeof(fp[0]), "%s%s_FHD_000.bmp", FC_OSD_SDCARD_PATH, fc_variant);
933+
snprintf(fp[1], sizeof(fp[1]), "%s%s_FHD_000.bmp", FC_OSD_LOCAL_PATH, fc_variant);
934+
snprintf(fp[2], sizeof(fp[2]), "%sBTFL_FHD_000.bmp", FC_OSD_LOCAL_PATH);
935935
} else {
936-
sprintf(fp[0], "%s%s_000.bmp", FC_OSD_SDCARD_PATH, fc_variant);
937-
sprintf(fp[1], "%s%s_000.bmp", FC_OSD_LOCAL_PATH, fc_variant);
938-
sprintf(fp[2], "%sBTFL_000.bmp", FC_OSD_LOCAL_PATH);
936+
snprintf(fp[0], sizeof(fp[0]), "%s%s_000.bmp", FC_OSD_SDCARD_PATH, fc_variant);
937+
snprintf(fp[1], sizeof(fp[1]), "%s%s_000.bmp", FC_OSD_LOCAL_PATH, fc_variant);
938+
snprintf(fp[2], sizeof(fp[2]), "%sBTFL_000.bmp", FC_OSD_LOCAL_PATH);
939939
}
940940

941941
// Optimized for runtime execution

src/core/settings.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,17 @@ const setting_t g_setting_defaults = {
227227
int settings_put_osd_element_shown(bool show, char *config_name) {
228228
char setting_key[128];
229229

230-
sprintf(setting_key, "element_%s_show", config_name);
230+
snprintf(setting_key, sizeof(setting_key), "element_%s_show", config_name);
231231
return settings_put_bool("osd", setting_key, show);
232232
}
233233

234234
int settings_put_osd_element_pos_x(const setting_osd_goggle_element_positions_t *pos, char *config_name) {
235235
char setting_key[128];
236236
int ret = 0;
237237

238-
sprintf(setting_key, "element_%s_pos_4_3_x", config_name);
238+
snprintf(setting_key, sizeof(setting_key), "element_%s_pos_4_3_x", config_name);
239239
ret = ini_putl("osd", setting_key, pos->mode_4_3.x, SETTING_INI);
240-
sprintf(setting_key, "element_%s_pos_16_9_x", config_name);
240+
snprintf(setting_key, sizeof(setting_key), "element_%s_pos_16_9_x", config_name);
241241
ret &= ini_putl("osd", setting_key, pos->mode_16_9.x, SETTING_INI);
242242
return ret;
243243
}
@@ -246,9 +246,9 @@ int settings_put_osd_element_pos_y(const setting_osd_goggle_element_positions_t
246246
char setting_key[128];
247247
int ret = 0;
248248

249-
sprintf(setting_key, "element_%s_pos_4_3_y", config_name);
249+
snprintf(setting_key, sizeof(setting_key), "element_%s_pos_4_3_y", config_name);
250250
ret = ini_putl("osd", setting_key, pos->mode_4_3.y, SETTING_INI);
251-
sprintf(setting_key, "element_%s_pos_16_9_y", config_name);
251+
snprintf(setting_key, sizeof(setting_key), "element_%s_pos_16_9_y", config_name);
252252
ret &= ini_putl("osd", setting_key, pos->mode_16_9.y, SETTING_INI);
253253
return ret;
254254
}
@@ -265,19 +265,19 @@ int settings_put_osd_element(const setting_osd_goggle_element_t *element, char *
265265
static void settings_load_osd_element(setting_osd_goggle_element_t *element, char *config_name, const setting_osd_goggle_element_t *defaults) {
266266
char buf[128];
267267

268-
sprintf(buf, "element_%s_show", config_name);
268+
snprintf(buf, sizeof(buf), "element_%s_show", config_name);
269269
element->show = settings_get_bool("osd", buf, defaults->show);
270270

271-
sprintf(buf, "element_%s_pos_4_3_x", config_name);
271+
snprintf(buf, sizeof(buf), "element_%s_pos_4_3_x", config_name);
272272
element->position.mode_4_3.x = ini_getl("osd", buf, defaults->position.mode_4_3.x, SETTING_INI);
273273

274-
sprintf(buf, "element_%s_pos_4_3_y", config_name);
274+
snprintf(buf, sizeof(buf), "element_%s_pos_4_3_y", config_name);
275275
element->position.mode_4_3.y = ini_getl("osd", buf, defaults->position.mode_4_3.y, SETTING_INI);
276276

277-
sprintf(buf, "element_%s_pos_16_9_x", config_name);
277+
snprintf(buf, sizeof(buf), "element_%s_pos_16_9_x", config_name);
278278
element->position.mode_16_9.x = ini_getl("osd", buf, defaults->position.mode_16_9.x, SETTING_INI);
279279

280-
sprintf(buf, "element_%s_pos_16_9_y", config_name);
280+
snprintf(buf, sizeof(buf), "element_%s_pos_16_9_y", config_name);
281281
element->position.mode_16_9.y = ini_getl("osd", buf, defaults->position.mode_16_9.y, SETTING_INI);
282282
}
283283

@@ -295,11 +295,11 @@ int settings_put_bool(char *section, char *key, bool value) {
295295
void settings_reset(void) {
296296
char buf[256];
297297

298-
sprintf(buf, "rm -f %s", SETTING_INI);
298+
snprintf(buf, sizeof(buf), "rm -f %s", SETTING_INI);
299299
system_exec(buf);
300300
usleep(50);
301301

302-
sprintf(buf, "touch %s", SETTING_INI);
302+
snprintf(buf, sizeof(buf), "touch %s", SETTING_INI);
303303
system_exec(buf);
304304
usleep(50);
305305

@@ -310,7 +310,7 @@ void settings_init(void) {
310310
// check if backup of old settings file exists after goggle update
311311
if (fs_file_exists("/mnt/UDISK/setting.ini")) {
312312
char buf[256];
313-
sprintf(buf, "cp -f /mnt/UDISK/setting.ini %s", SETTING_INI);
313+
snprintf(buf, sizeof(buf), "cp -f /mnt/UDISK/setting.ini %s", SETTING_INI);
314314
system_exec(buf);
315315
usleep(10);
316316
system_exec("rm /mnt/UDISK/setting.ini");

src/driver/gpio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void gpio_open(int port_num) {
3636
}
3737

3838
char buf[64];
39-
sprintf(buf, "/sys/class/gpio/gpio%d/direction", port_num);
39+
snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", port_num);
4040

4141
if (!fs_printf(buf, "out")) {
4242
return;
@@ -45,6 +45,6 @@ void gpio_open(int port_num) {
4545

4646
void gpio_set(int port_num, bool val) {
4747
char buf[64];
48-
sprintf(buf, "/sys/class/gpio/gpio%d/value", port_num);
48+
snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", port_num);
4949
fs_printf(buf, "%d", val ? 1 : 0);
5050
}

src/driver/nct75.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int nct_read_temperature(nct_type_t type) {
2727

2828
int dev_id = type + (getHwRevision() == HW_REV_1 ? 1 : 0);
2929

30-
sprintf(buf, "/sys/bus/iio/devices/iio:device%d/in_voltage0_raw", dev_id);
30+
snprintf(buf, sizeof(buf), "/sys/bus/iio/devices/iio:device%d/in_voltage0_raw", dev_id);
3131
fp = fopen(buf, "r");
3232
if (!fp) {
3333
static bool bFirst = true;

src/lang/language.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bool language_config() {
5959
int i = 0;
6060

6161
for (i = 0; i < LANG_END; i++) {
62-
sprintf(buf, "/mnt/extsd/%s", language_config_file[i]);
62+
snprintf(buf, sizeof(buf), "/mnt/extsd/%s", language_config_file[i]);
6363
if (access(buf, F_OK) == 0) {
6464
LOGI("%s found", language_config_file[i]);
6565
ini_putl("language", "lang", i, SETTING_INI);

src/record/confparser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ void conf_loadRecordParams(char* confFile, RecordParams_t* para)
404404
memset(para->diskPath, 0, sizeof(para->packPath));
405405
strcpy(para->diskPath, sTemp);
406406
memset(para->packPath, 0, sizeof(para->packPath));
407-
sprintf(para->packPath, "%s%s", para->diskPath, REC_packPATH);
407+
snprintf(para->packPath, MAX_pathLEN, "%s%s", para->diskPath, REC_packPATH);
408408
}
409409

410410
lValue = ini_gets(SEC_RECORD, KEY_TYPE, REC_packTYPE, sTemp, sizearray(sTemp), confFile);

src/record/main.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ int already_running(void)
121121
exit(1);
122122
}
123123
ftruncate(fd, 0);
124-
sprintf(buf, "%ld", (long)getpid());
124+
snprintf(buf, sizeof(buf), "%ld", (long)getpid());
125125
write(fd, buf, strlen(buf)+1);
126126
return(0);
127127
}
@@ -203,7 +203,7 @@ void record_saveStatus(RecordContext_t* recCtx, RecordStatus_e recStatus)
203203
LOGE( "can't lock %s: %s", REC_dataFILE, strerror(errno));
204204
}
205205
ftruncate(fd, 0);
206-
sprintf(buf, "%d", recCtx->status);
206+
snprintf(buf, sizeof(buf), "%d", recCtx->status);
207207
write(fd, buf, strlen(buf)+1);
208208
close(fd);
209209

@@ -354,13 +354,13 @@ int record_start(RecordContext_t* recCtx)
354354
char sFile[256];
355355
switch (recCtx->params.fileNaming) {
356356
case NAMING_CONTIGUOUS:
357-
REC_filePathGet(sFile, recCtx->params.packPath, REC_packPREFIX, nbFileIndex, recCtx->params.packType);
357+
REC_filePathGet(sFile, sizeof(sFile), recCtx->params.packPath, REC_packPREFIX, nbFileIndex, recCtx->params.packType);
358358
break;
359359
case NAMING_DATE: {
360360
const time_t t = time(0);
361361
const struct tm* date = localtime(&t);
362-
sprintf(dateString, "%04d%02d%02d-%02d%02d%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday, date->tm_hour, date->tm_min, date->tm_sec);
363-
sprintf(sFile, "%s%s.%s", recCtx->params.packPath, dateString, recCtx->params.packType);
362+
snprintf(dateString, sizeof(dateString), "%04d%02d%02d-%02d%02d%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday, date->tm_hour, date->tm_min, date->tm_sec);
363+
snprintf(sFile, sizeof(sFile), "%s%s.%s", recCtx->params.packPath, dateString, recCtx->params.packType);
364364
break;
365365
}
366366
}
@@ -429,7 +429,7 @@ int record_start(RecordContext_t* recCtx)
429429
strcpy(fileName, strrchr(sFile, '/') + 1);
430430
av_dict_set(&ff->ofmtContext->metadata, "title", fileName, 0);
431431

432-
sprintf(localDateString, "%04d-%02d-%02d %02d:%02d:%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday, date->tm_hour, date->tm_min, date->tm_sec);
432+
snprintf(localDateString, sizeof(localDateString), "%04d-%02d-%02d %02d:%02d:%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday, date->tm_hour, date->tm_min, date->tm_sec);
433433
av_dict_set(&ff->ofmtContext->metadata, "date", localDateString, 0);
434434
}
435435

@@ -462,10 +462,10 @@ int record_start(RecordContext_t* recCtx)
462462

463463
switch (recCtx->params.fileNaming) {
464464
case NAMING_CONTIGUOUS:
465-
REC_filePathGet(sFile, recCtx->params.packPath, REC_packPREFIX, nbFileIndex, REC_packSnapTYPE);
465+
REC_filePathGet(sFile, sizeof(sFile), recCtx->params.packPath, REC_packPREFIX, nbFileIndex, REC_packSnapTYPE);
466466
break;
467467
case NAMING_DATE:
468-
sprintf(sFile, "%s%s.%s", recCtx->params.packPath, dateString, REC_packSnapTYPE);
468+
snprintf(sFile, sizeof(sFile), "%s%s.%s", recCtx->params.packPath, dateString, REC_packSnapTYPE);
469469
break;
470470
}
471471
ret = record_takePicture(recCtx, sFile);
@@ -519,7 +519,7 @@ bool record_pack(RecordContext_t* recCtx)
519519
VencSpspps_t veHeader = { NULL, 0 };
520520
int nbFileIndex = recCtx->nbFileIndex;
521521
char sFile[256];
522-
REC_filePathGet(sFile, recCtx->params.packPath, REC_packPREFIX, nbFileIndex, recCtx->params.packType);
522+
REC_filePathGet(sFile, sizeof(sFile), recCtx->params.packPath, REC_packPREFIX, nbFileIndex, recCtx->params.packType);
523523

524524
FFPack_t* ff = ffpack_openFile(sFile, NULL);
525525
if( ff == NULL ) {
@@ -577,7 +577,7 @@ bool record_pack(RecordContext_t* recCtx)
577577

578578
pthread_mutex_unlock(&recCtx->mutex);
579579

580-
REC_filePathGet(sFile, recCtx->params.packPath, REC_packPREFIX, nbFileIndex, REC_packSnapTYPE);
580+
REC_filePathGet(sFile, sizeof(sFile), recCtx->params.packPath, REC_packPREFIX, nbFileIndex, REC_packSnapTYPE);
581581
record_takePicture(recCtx, sFile);
582582

583583
return true;
@@ -888,7 +888,7 @@ void record_checkConf(RecordContext_t* recCtx, char* confSet)
888888
readlink("/proc/self/exe", sTemp, MAX_pathLEN);
889889
p = strrchr(sTemp,'/');
890890
*p = '\0';
891-
sprintf(recCtx->confFile, "%s/%s", sTemp, REC_confFILE);
891+
snprintf(recCtx->confFile, MAX_pathLEN, "%s/%s", sTemp, REC_confFILE);
892892
}
893893
}
894894

src/record/record_definitions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ extern "C" {
9191
#define REC_packTypesNUM 2
9292
#define REC_starFORMAT "%u:%02u star\n"
9393

94-
#define REC_filePathGet(BUFF, PATH, PREFIX, INDEX, FILEFMT) \
95-
sprintf((BUFF), "%s%s%04d.%s", (PATH), (PREFIX), (INDEX), (FILEFMT));
94+
#define REC_filePathGet(BUFF, MAXLEN, PATH, PREFIX, INDEX, FILEFMT) \
95+
snprintf((BUFF), (MAXLEN), "%s%s%04d.%s", (PATH), (PREFIX), (INDEX), (FILEFMT));
9696

9797
#define ZeroMemory(p, size) memset(p, 0, size)
9898

src/rtspLive/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static int already_running(void)
129129
exit(1);
130130
}
131131
ftruncate(fd, 0);
132-
sprintf(buf, "%ld", (long)getpid());
132+
snprintf(buf, sizeof(buf), "%ld", (long)getpid());
133133
write(fd, buf, strlen(buf)+1);
134134
return(0);
135135
}
@@ -179,7 +179,7 @@ void live_checkConf(LiveContext_t* liveCtx, char* confSet)
179179
readlink("/proc/self/exe", sTemp, MAX_pathLEN);
180180
p = strrchr(sTemp,'/');
181181
*p = '\0';
182-
sprintf(liveCtx->confFile, "%s/%s", sTemp, REC_confFILE);
182+
snprintf(liveCtx->confFile, MAX_pathLEN, "%s/%s", sTemp, REC_confFILE);
183183
}
184184
}
185185

0 commit comments

Comments
 (0)