Refactored all the sprintf to use snprintf instead. (#628)

This commit is contained in:
Steve Karg
2024-04-27 12:41:45 -05:00
committed by GitHub
parent 70c54817fd
commit bb276e2431
46 changed files with 308 additions and 272 deletions
+3 -3
View File
@@ -94,12 +94,12 @@ uint32_t Analog_Input_Index_To_Instance(unsigned index)
bool Analog_Input_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
static char text_string[32]; /* okay for single thread */
static char text[32]; /* okay for single thread */
bool status = false;
if (object_instance < MAX_ANALOG_INPUTS) {
sprintf(text_string, "AI-%lu", object_instance);
status = characterstring_init_ansi(object_name, text_string);
snprintf(text, sizeof(text), "AI-%lu", (unsigned long)object_instance);
status = characterstring_init_ansi(object_name, text);
}
return status;
+3 -3
View File
@@ -158,14 +158,14 @@ bool Analog_Value_Present_Value_Set(
bool Analog_Value_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
static char text_string[32] = ""; /* okay for single thread */
static char text[32] = ""; /* okay for single thread */
unsigned index = 0;
bool status = false;
index = Analog_Value_Instance_To_Index(object_instance);
if (index < MAX_ANALOG_VALUES) {
sprintf(text_string, "AV-%lu", object_instance);
status = characterstring_init_ansi(object_name, text_string);
snprintf(text, sizeof(text), "AV-%lu", (unsigned long)object_instance);
status = characterstring_init_ansi(object_name, text);
}
return status;
+1 -1
View File
@@ -64,7 +64,7 @@ void SerialPort::openChannel()
/* For COM ports greater than 9 you have to use a special syntax
for CreateFile. The syntax also works for COM ports 1-9. */
/* http://support.microsoft.com/kb/115831 */
sprintf(comName, "\\\\.\\COM%ld", portNumber);
snprintf(comName, sizeof(comName), "\\\\.\\COM%ld", portNumber);
}
serialHandle = CreateFile( comName, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
+3 -3
View File
@@ -139,12 +139,12 @@ bool Binary_Input_Present_Value_Set(
bool Binary_Input_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
static char text_string[32]; /* okay for single thread */
static char text[32]; /* okay for single thread */
bool status = false;
if (object_instance < MAX_BINARY_INPUTS) {
sprintf(text_string, "BI-%lu", object_instance);
status = characterstring_init_ansi(object_name, text_string);
snprintf(text, sizeof(text), "BI-%lu", (unsigned long)object_instance);
status = characterstring_init_ansi(object_name, text);
}
return status;
+3 -3
View File
@@ -209,12 +209,12 @@ bool Binary_Output_Out_Of_Service(uint32_t instance)
bool Binary_Output_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
static char text_string[32]; /* okay for single thread */
static char text[32]; /* okay for single thread */
bool status = false;
if (object_instance < MAX_BINARY_OUTPUTS) {
sprintf(text_string, "BO-%lu", object_instance);
status = characterstring_init_ansi(object_name, text_string);
snprintf(text, sizeof(text), "BO-%lu", (unsigned long)object_instance);
status = characterstring_init_ansi(object_name, text);
}
return status;
+4 -3
View File
@@ -284,11 +284,12 @@ uint32_t Device_Index_To_Instance(unsigned index)
static char *Device_Name_Default(void)
{
static char text_string[32]; /* okay for single thread */
static char text[32]; /* okay for single thread */
sprintf(text_string, "DEVICE-%lu", Object_Instance_Number);
snprintf(text, sizeof(text), "DEVICE-%lu",
(unsigned long)Object_Instance_Number);
return text_string;
return text;
}
bool Device_Object_Name(
+5 -5
View File
@@ -154,7 +154,7 @@ void test_task(void)
if (mstimer_expired(&Test_Timer)) {
mstimer_reset(&Test_Timer);
sprintf(Send_Buffer, "BACnet: 0000000\r\n");
snprintf(Send_Buffer, sizeof(Send_Buffer), "BACnet: 0000000\r\n");
MSTP_MAC_Address = input_address();
Send_Buffer[8] = (MSTP_MAC_Address & BIT(0)) ? '1' : '0';
Send_Buffer[9] = (MSTP_MAC_Address & BIT(1)) ? '1' : '0';
@@ -198,17 +198,17 @@ void test_task(void)
break;
case 'e':
seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *)&id, 2);
sprintf(Send_Buffer, "\r\n%04X", id);
snprintf(Send_Buffer, sizeof(Send_Buffer), "\r\n%04X", id);
serial_bytes_send((uint8_t *)Send_Buffer, strlen(Send_Buffer));
break;
case 'b':
sprintf(Send_Buffer, "\r\n%lubps",
snprintf(Send_Buffer, sizeof(Send_Buffer), "\r\n%lubps",
(unsigned long)rs485_baud_rate());
serial_bytes_send((uint8_t *)Send_Buffer, strlen(Send_Buffer));
break;
case 'm':
sprintf(
Send_Buffer, "\r\nMax:%u", (unsigned)dlmstp_max_master());
snprintf(Send_Buffer, sizeof(Send_Buffer),
"\r\nMax:%u", (unsigned)dlmstp_max_master());
serial_bytes_send((uint8_t *)Send_Buffer, strlen(Send_Buffer));
break;
default: