Skip to content

Commit f228451

Browse files
Li.TianmingYour Name
authored andcommitted
Change sprintf() to snprintf()
1 parent fcf15fe commit f228451

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

src/drivers/osd/msp_osd/uorb_to_msp.cpp

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,7 @@ msp_rendor_rssi_t construct_rendor_RSSI(const input_rc_s &input_rc)
214214
rssi.screenYPosition = 0x02;
215215
rssi.screenXPosition = 0x02;
216216

217-
char rssi_str[5] = {0};
218-
int len = sprintf(rssi_str, "%d", input_rc.link_quality);
219-
strncpy(&rssi.str[0], rssi_str, 4);
217+
int len = snprintf(&rssi.str[0], sizeof(rssi.str) - 1, "%d", input_rc.link_quality);
220218

221219
if (len >= 3) {
222220
rssi.str[3] = '%';
@@ -277,7 +275,7 @@ msp_rendor_battery_state_t construct_rendor_BATTERY_STATE(const battery_status_s
277275
battery_state.iconIndex = 0x96; // Dead battery Icon
278276
}
279277

280-
sprintf(&battery_state.str[0], "%.1fV", (double)sigle_cell_v);
278+
snprintf(&battery_state.str[0], sizeof(battery_state.str), "%.1fV", (double)sigle_cell_v);
281279
return battery_state;
282280
}
283281

@@ -347,10 +345,10 @@ msp_rendor_latitude_t construct_rendor_GPS_LAT(const sensor_gps_s &vehicle_gps_p
347345
lat.screenXPosition = 0x29;
348346

349347
if (vehicle_gps_position.fix_type >= 2) {
350-
sprintf(&lat.str[0], "%.6f", vehicle_gps_position.latitude_deg);
348+
snprintf(&lat.str[0], sizeof(lat.str), "%.6f", vehicle_gps_position.latitude_deg);
351349

352350
} else {
353-
sprintf(&lat.str[0], "%.6f", 0.0);
351+
snprintf(&lat.str[0], sizeof(lat.str), "%.6f", 0.0);
354352
}
355353

356354
return lat;
@@ -364,10 +362,10 @@ msp_rendor_longitude_t construct_rendor_GPS_LON(const sensor_gps_s &vehicle_gps_
364362
lon.screenXPosition = 0x29;
365363

366364
if (vehicle_gps_position.fix_type >= 2) {
367-
sprintf(&lon.str[0], "%.6f", vehicle_gps_position.longitude_deg);
365+
snprintf(&lon.str[0], sizeof(lon.str), "%.6f", vehicle_gps_position.longitude_deg);
368366

369367
} else {
370-
sprintf(&lon.str[0], "%.6f", -0.0);
368+
snprintf(&lon.str[0], sizeof(lon.str), "%.6f", -0.0);
371369
}
372370

373371
return lon;
@@ -379,9 +377,8 @@ msp_rendor_satellites_used_t construct_rendor_GPS_NUM(const sensor_gps_s &vehicl
379377

380378
num.screenYPosition = 0x08;
381379
num.screenXPosition = 0x29;
382-
char num_str[4];
383-
sprintf(num_str, "%d", vehicle_gps_position.satellites_used % 100); // max 99
384-
strncpy(&num.str[0], num_str, 2);
380+
381+
snprintf(&num.str[0], sizeof(num.str), "%d", vehicle_gps_position.satellites_used);
385382

386383
return num;
387384
}
@@ -443,13 +440,9 @@ msp_rendor_distanceToHome_t construct_rendor_distanceToHome(const home_position_
443440

444441
dist_i = (int16_t)distance_to_home; // meters
445442

446-
} else {
447-
dist_i = 0; // meters
448443
}
449444

450-
char num_str[7];
451-
sprintf(num_str, "%d", dist_i); // 65536
452-
strncpy(&distance.str[0], num_str, 6);
445+
snprintf(&distance.str[0], sizeof(distance.str), "%d", dist_i); // 65536
453446

454447
return distance;
455448
}
@@ -491,9 +484,7 @@ msp_rendor_pitch_t construct_rendor_PITCH(const vehicle_attitude_s &vehicle_att
491484
double pitch_deg = (double)math::degrees(euler_attitude.theta());
492485
// attitude.roll = math::degrees(euler_attitude.phi()) * 10;
493486

494-
char pitch_str[10];
495-
sprintf(pitch_str, "%.1f", pitch_deg);
496-
strncpy(&pit.str[0], pitch_str, 5);
487+
snprintf(&pit.str[0], sizeof(pit.str), "%.1f", pitch_deg);
497488

498489
return pit;
499490
}
@@ -511,9 +502,7 @@ msp_rendor_roll_t construct_rendor_ROLL(const vehicle_attitude_s &vehicle_attit
511502
// double pitch = (double)math::degrees(euler_attitude.theta());
512503
double roll_deg = (double)math::degrees(euler_attitude.phi());
513504

514-
char roll_str[10];
515-
sprintf(roll_str, "%.1f", roll_deg);
516-
strncpy(&roll.str[0], roll_str, 5);
505+
snprintf(&roll.str[0], sizeof(roll.str), "%.1f", roll_deg);
517506

518507
return roll;
519508
}
@@ -559,9 +548,7 @@ msp_rendor_altitude_t construct_Rendor_ALTITUDE(const sensor_gps_s &vehicle_gps_
559548
alt = vehicle_local_position.z * -1;
560549
}
561550

562-
char alt_str[10];
563-
sprintf(alt_str, "%.1f", alt);
564-
strncpy(&altitude.str[0], alt_str, 6);
551+
snprintf(&altitude.str[0], sizeof(altitude.str), "%.1f", alt);
565552

566553
return altitude;
567554
}

0 commit comments

Comments
 (0)