From 00b96ad08dcbf339f87a732782b7fd232351de4c Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Tue, 9 Jan 2024 22:19:27 +0100 Subject: [PATCH] emess(): avoid potential unsafe uses --- src/apps/cs2cs.cpp | 4 ++-- src/apps/emess.h | 9 ++++++++- src/apps/proj.cpp | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/apps/cs2cs.cpp b/src/apps/cs2cs.cpp index e9baa726e1..b6a1ed123b 100644 --- a/src/apps/cs2cs.cpp +++ b/src/apps/cs2cs.cpp @@ -899,7 +899,7 @@ int main(int argc, char **argv) { sourceEpochDbl = c_locale_stod(sourceEpoch); } catch (const std::exception &e) { sourceEpochDbl = 0; - emess(3, e.what()); + emess(3, "%s", e.what()); } srcMetadata = proj_coordinate_metadata_create(nullptr, src, sourceEpochDbl); @@ -917,7 +917,7 @@ int main(int argc, char **argv) { targetEpochDbl = c_locale_stod(targetEpoch); } catch (const std::exception &e) { targetEpochDbl = 0; - emess(3, e.what()); + emess(3, "%s", e.what()); } dstMetadata = proj_coordinate_metadata_create(nullptr, dst, targetEpochDbl); diff --git a/src/apps/emess.h b/src/apps/emess.h index b09262e5f3..68a50fd472 100644 --- a/src/apps/emess.h +++ b/src/apps/emess.h @@ -19,6 +19,13 @@ extern struct EMESS emess_dat; #endif /* use type */ -void emess(int, const char *, ...); +#if defined(__GNUC__) +#define EMESS_PRINT_FUNC_FORMAT(format_idx, arg_idx) \ + __attribute__((__format__(__printf__, format_idx, arg_idx))) +#else +#define EMESS_PRINT_FUNC_FORMAT(format_idx, arg_idx) +#endif + +void emess(int, const char *, ...) EMESS_PRINT_FUNC_FORMAT(2, 3); #endif /* end EMESS_H */ diff --git a/src/apps/proj.cpp b/src/apps/proj.cpp index c3e5b1c57d..3fa94e75b9 100644 --- a/src/apps/proj.cpp +++ b/src/apps/proj.cpp @@ -279,7 +279,7 @@ static void vprocess(FILE *fid) { } if (proj_context_errno(nullptr)) { - emess(-1, proj_errno_string(proj_context_errno(nullptr))); + emess(-1, "%s", proj_errno_string(proj_context_errno(nullptr))); continue; }