Change sprintf to SNPRINTF in misc.c

https://github.com/Hamlib/Hamlib/issues/857
pull/943/head
Mike Black W9MDB 2022-01-18 15:03:11 -06:00
rodzic 0e5962a816
commit f801244fb6
2 zmienionych plików z 8 dodań i 5 usunięć

Wyświetl plik

@ -315,7 +315,7 @@ size_t HAMLIB_API to_hex(size_t source_length, const unsigned char *source_data,
for (i = 0; i < length; i++) for (i = 0; i < length; i++)
{ {
sprintf(dest, "%02X", source[0]); SNPRINTF(dest, dest_length - 2*i, "%02X", source[0]);
source++; source++;
dest += 2; dest += 2;
} }
@ -382,11 +382,12 @@ int millis_to_dot10ths(int millis, int wpm)
* pretty print frequencies * pretty print frequencies
* str must be long enough. max can be as long as 17 chars * str must be long enough. max can be as long as 17 chars
*/ */
int HAMLIB_API sprintf_freq(char *str, int nlen, freq_t freq) int HAMLIB_API sprintf_freq(char *str, int str_len, freq_t freq)
{ {
double f; double f;
char *hz; char *hz;
int decplaces = 10; int decplaces = 10;
int retval;
// too verbose // too verbose
//rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__); //rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
@ -415,7 +416,9 @@ int HAMLIB_API sprintf_freq(char *str, int nlen, freq_t freq)
decplaces = 1; decplaces = 1;
} }
return sprintf(str, "%.*f %s", decplaces, f, hz); SNPRINTF(str, str_len, "%.*f %s", decplaces, f, hz);
retval = strlen(str);
return retval;
} }
@ -2061,7 +2064,7 @@ int HAMLIB_API parse_hoststr(char *hoststr, char host[256], char port[6])
if (sscanf(hoststr, ":%5[0-9]%1s", port, if (sscanf(hoststr, ":%5[0-9]%1s", port,
dummy) == 1) // just a port if you please dummy) == 1) // just a port if you please
{ {
sprintf(hoststr, "%s:%s\n", "localhost", port); snprintf(hoststr, sizeof(hoststr), "%s:%s\n", "localhost", port);
rig_debug(RIG_DEBUG_VERBOSE, "%s: hoststr=%s\n", __func__, hoststr); rig_debug(RIG_DEBUG_VERBOSE, "%s: hoststr=%s\n", __func__, hoststr);
return RIG_OK; return RIG_OK;
} }

Wyświetl plik

@ -81,7 +81,7 @@ extern HAMLIB_EXPORT(double) morse_code_dot_to_millis(int wpm);
extern HAMLIB_EXPORT(int) dot10ths_to_millis(int dot10ths, int wpm); extern HAMLIB_EXPORT(int) dot10ths_to_millis(int dot10ths, int wpm);
extern HAMLIB_EXPORT(int) millis_to_dot10ths(int millis, int wpm); extern HAMLIB_EXPORT(int) millis_to_dot10ths(int millis, int wpm);
extern HAMLIB_EXPORT(int) sprintf_freq(char *str, int len, freq_t); extern HAMLIB_EXPORT(int) sprintf_freq(char *str, int str_len, freq_t);
/* flag that determines if AI mode should be restored on exit on /* flag that determines if AI mode should be restored on exit on
applicable rigs - See rig_no_restore_ai() */ applicable rigs - See rig_no_restore_ai() */