[deprecation] replace all usages of sprintf

This commit is contained in:
akallabeth 2024-11-11 18:01:42 +01:00
parent 7797d8d669
commit db07add07a
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5
5 changed files with 61 additions and 55 deletions

View File

@ -1615,7 +1615,8 @@ static int udev_get_device_handle(URBDRC_PLUGIN* urbdrc, libusb_context* ctx, UD
error = 0;
WLog_Print(urbdrc->log, WLOG_DEBUG, " Port: %d", pdev->port_number);
/* gen device path */
(void)sprintf(pdev->path, "%" PRIu16 "-%d", bus_number, pdev->port_number);
(void)_snprintf(pdev->path, sizeof(pdev->path), "%" PRIu16 "-%d", bus_number,
pdev->port_number);
WLog_Print(urbdrc->log, WLOG_DEBUG, " DevPath: %s", pdev->path);
}

View File

@ -71,23 +71,19 @@
#endif // GOOGLE_PROFILER
extern float measure_delta_time(UINT64 t0, UINT64 t1);
extern void measure_floatprint(float t, char* output);
extern void measure_floatprint(float t, char* output, size_t len);
#define MEASURE_LOOP_START(_prefix_, _count_) \
{ \
UINT64 _start, _stop; \
char* _prefix; \
int _count = (_count_); \
int _loop; \
float _delta; \
char _str1[32], _str2[32]; \
_prefix = _strdup(_prefix_); \
_str1[0] = '\0'; \
_str2[0] = '\0'; \
_start = winpr_GetTickCount64NS(); \
PROFILER_START(_prefix); \
_loop = (_count); \
do \
#define MEASURE_LOOP_START(_prefix_, _count_) \
{ \
int _count = (_count_); \
int _loop; \
char str1[32] = { 0 }; \
char str2[32] = { 0 }; \
char* _prefix = _strdup(_prefix_); \
const UINT64 start = winpr_GetTickCount64NS(); \
PROFILER_START(_prefix); \
_loop = (_count); \
do \
{
#define MEASURE_LOOP_STOP \
@ -95,33 +91,33 @@ extern void measure_floatprint(float t, char* output);
while (--_loop) \
;
#define MEASURE_GET_RESULTS(_result_) \
PROFILER_STOP; \
_stop = winpr_GetTickCount64NS(); \
_delta = measure_delta_time(_start, _stop); \
(_result_) = (float)_count / _delta; \
free(_prefix); \
#define MEASURE_GET_RESULTS(_result_) \
PROFILER_STOP; \
const UINT64 stop = winpr_GetTickCount64NS(); \
const float delta = measure_delta_time(start, stop); \
(_result_) = (float)_count / delta; \
free(_prefix); \
}
#define MEASURE_SHOW_RESULTS(_result_) \
PROFILER_STOP; \
_stop = winpr_GetTickCount64NS(); \
_delta = measure_delta_time(_start, _stop); \
(_result_) = (float)_count / _delta; \
measure_floatprint((float)_count / _delta, _str1); \
printf("%s: %9d iterations in %5.1f seconds = %s/s \n", _prefix, _count, _delta, _str1); \
free(_prefix); \
#define MEASURE_SHOW_RESULTS(_result_) \
PROFILER_STOP; \
const UINT64 stop = winpr_GetTickCount64NS(); \
const float delta = measure_delta_time(start, stop); \
(_result_) = (float)_count / delta; \
measure_floatprint((float)_count / delta, str1); \
printf("%s: %9d iterations in %5.1f seconds = %s/s \n", _prefix, _count, delta, str1); \
free(_prefix); \
}
#define MEASURE_SHOW_RESULTS_SCALED(_scale_, _label_) \
PROFILER_STOP; \
_stop = winpr_GetTickCount64NS(); \
_delta = measure_delta_time(_start, _stop); \
measure_floatprint((float)_count / _delta, _str1); \
measure_floatprint((float)_count / _delta * (_scale_), _str2); \
printf("%s: %9d iterations in %5.1f seconds = %s/s = %s%s \n", _prefix, _count, _delta, _str1, \
_str2, _label_); \
free(_prefix); \
#define MEASURE_SHOW_RESULTS_SCALED(_scale_, _label_) \
PROFILER_STOP; \
const UINT64 stop = winpr_GetTickCount64NS(); \
const float delta = measure_delta_time(start, stop); \
measure_floatprint((float)_count / delta, str1); \
measure_floatprint((float)_count / delta * (_scale_), str2); \
printf("%s: %9d iterations in %5.1f seconds = %s/s = %s%s \n", _prefix, _count, delta, str1, \
str2, _label_); \
free(_prefix); \
}
#define MEASURE_TIMED(_label_, _init_iter_, _test_time_, _result_, _call_) \

View File

@ -44,7 +44,7 @@ float measure_delta_time(UINT64 t0, UINT64 t1)
}
/* ------------------------------------------------------------------------- */
void measure_floatprint(float t, char* output)
void measure_floatprint(float t, char* output, size_t len)
{
/* I don't want to link against -lm, so avoid log,exp,... */
float f = 10.0;
@ -57,19 +57,20 @@ void measure_floatprint(float t, char* output)
i = ((int)(t / f + 0.5f)) * (int)f;
if (t < 0.0f)
(void)sprintf(output, "%f", t);
(void)_snprintf(output, len, "%f", t);
else if (i == 0)
(void)sprintf(output, "%d", (int)(t + 0.5f));
(void)_snprintf(output, len, "%d", (int)(t + 0.5f));
else if (t < 1e+3f)
(void)sprintf(output, "%3d", i);
(void)_snprintf(output, len, "%3d", i);
else if (t < 1e+6f)
(void)sprintf(output, "%3d,%03d", i / 1000, i % 1000);
(void)_snprintf(output, len, "%3d,%03d", i / 1000, i % 1000);
else if (t < 1e+9f)
(void)sprintf(output, "%3d,%03d,000", i / 1000000, (i % 1000000) / 1000);
(void)_snprintf(output, len, "%3d,%03d,000", i / 1000000, (i % 1000000) / 1000);
else if (t < 1e+12f)
(void)sprintf(output, "%3d,%03d,000,000", i / 1000000000, (i % 1000000000) / 1000000);
(void)_snprintf(output, len, "%3d,%03d,000,000", i / 1000000000,
(i % 1000000000) / 1000000);
else
(void)sprintf(output, "%f", t);
(void)_snprintf(output, len, "%f", t);
}
void prim_test_setup(BOOL performance)

View File

@ -204,7 +204,7 @@ int winpr_vasprintf(char** s, size_t* slen, WINPR_FORMAT_ARG const char* templ,
return -1;
va_copy(ap, oap);
const int plen = vsprintf(str, templ, ap);
const int plen = vsnprintf(str, (size_t)length, templ, ap);
va_end(ap);
if (length != plen)

View File

@ -44,6 +44,11 @@
struct format_option_recurse;
struct format_tid_arg
{
char tid[32];
};
struct format_option
{
const char* fmt;
@ -90,7 +95,9 @@ static void WLog_PrintMessagePrefix(wLog* log, wLogMessage* message,
static const char* get_tid(void* arg)
{
char* str = arg;
struct format_tid_arg* targ = arg;
WINPR_ASSERT(targ);
size_t tid = 0;
#if defined __linux__ && !defined ANDROID
/* On Linux we prefer to see the LWP id */
@ -98,8 +105,8 @@ static const char* get_tid(void* arg)
#else
tid = (size_t)GetCurrentThreadId();
#endif
(void)sprintf(str, "%08" PRIxz, tid);
return str;
(void)_snprintf(targ->tid, sizeof(targ->tid), "%08" PRIxz, tid);
return targ->tid;
}
static BOOL log_invalid_fmt(const char* what)
@ -230,7 +237,8 @@ BOOL WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout, wLogMessage* me
WINPR_ASSERT(layout);
WINPR_ASSERT(message);
char tid[32] = { 0 };
struct format_tid_arg targ = { 0 };
SYSTEMTIME localTime = { 0 };
GetLocalTime(&localTime);
@ -266,8 +274,8 @@ BOOL WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout, wLogMessage* me
{ ENTRY("%pid"), ENTRY("%u"), NULL, (void*)(size_t)GetCurrentProcessId(), NULL,
&recurse }, /* process id */
{ ENTRY("%se"), ENTRY("%02u"), NULL, (void*)(size_t)localTime.wSecond, NULL,
&recurse }, /* seconds */
{ ENTRY("%tid"), ENTRY("%s"), get_tid, tid, NULL, &recurse }, /* thread id */
&recurse }, /* seconds */
{ ENTRY("%tid"), ENTRY("%s"), get_tid, &targ, NULL, &recurse }, /* thread id */
{ ENTRY("%yr"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wYear, NULL,
&recurse }, /* year */
{ ENTRY("%{"), ENTRY("%}"), NULL, log->context, skip_if_null,