From dad29bfae71d6cdd8397767362deef5b83487d87 Mon Sep 17 00:00:00 2001 From: pfeatherstone <45853521+pfeatherstone@users.noreply.github.com> Date: Tue, 19 Sep 2023 13:12:02 +0100 Subject: [PATCH] Apple Clang complains about sprintf. Use snprintf instead. (#2862) Co-authored-by: Your Name --- dlib/gui_core/gui_core_kernel_2.cpp | 4 ++-- dlib/gui_widgets/nativefont.h | 11 +++++------ dlib/http_client/http_client.cpp | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/dlib/gui_core/gui_core_kernel_2.cpp b/dlib/gui_core/gui_core_kernel_2.cpp index d0c8879eb..be1cfb8d1 100644 --- a/dlib/gui_core/gui_core_kernel_2.cpp +++ b/dlib/gui_core/gui_core_kernel_2.cpp @@ -1614,9 +1614,9 @@ namespace dlib char **mlist; int mcount; char *def_str; - char fontset[256]; + char fontset[256] = {0}; const long native_font_height = 12; - sprintf(fontset, "-*-*-medium-r-normal--%lu-*-*-*-", native_font_height); + snprintf(fontset, sizeof(fontset), "-*-*-medium-r-normal--%lu-*-*-*-", native_font_height); x11_stuff.fs = XCreateFontSet(x11_stuff.globals->disp, fontset, &mlist, &mcount, &def_str); xpoint.x = 0; xpoint.y = 0; diff --git a/dlib/gui_widgets/nativefont.h b/dlib/gui_widgets/nativefont.h index 4850b79fe..69f0cfade 100644 --- a/dlib/gui_widgets/nativefont.h +++ b/dlib/gui_widgets/nativefont.h @@ -345,14 +345,13 @@ namespace nativefont cmap = DefaultColormap(d, DefaultScreen(d)); } - char fontset[256]; + char fontset[256] = {0}; { - char *p = fontset; - p += sprintf(fontset, "-*-*-%s-%c-normal--%d-*-*-*-%c", - bold ? "bold" : "medium", italic ? 'i' : 'r', height_want, fixed ? 'c' : 'p'); + int offset = snprintf(fontset, sizeof(fontset), "-*-*-%s-%c-normal--%d-*-*-*-%c", + bold ? "bold" : "medium", italic ? 'i' : 'r', height_want, fixed ? 'c' : 'p'); if (fixed){ - sprintf(p, ",-*-*-%s-%c-normal--%d-*-*-*-m", - bold ? "bold" : "medium", italic ? 'i' : 'r', height_want); + snprintf(&fontset[offset], sizeof(fontset) - offset, ",-*-*-%s-%c-normal--%d-*-*-*-m", + bold ? "bold" : "medium", italic ? 'i' : 'r', height_want); } } bool equal_font; diff --git a/dlib/http_client/http_client.cpp b/dlib/http_client/http_client.cpp index cb1105449..078cdfa4a 100644 --- a/dlib/http_client/http_client.cpp +++ b/dlib/http_client/http_client.cpp @@ -119,7 +119,7 @@ namespace dlib #ifdef __WXMSW__ ::ltoa(header_value, buf, 10); #else - sprintf(buf, "%ld", header_value); + snprintf(buf, sizeof(buf), "%ld", header_value); #endif set_header(header_name, buf); } @@ -154,7 +154,7 @@ namespace dlib #ifdef __WXMSW__ ::ltoa(cookie_value, buf, 10); #else - sprintf(buf, "%ld", cookie_value); + snprintf(buf, sizeof(buf), "%ld", cookie_value); #endif set_cookie(cookie_name, buf); }