From 8b36c193fd5709ce7234059a3a96b2ba42f7e868 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 24 Aug 2023 12:17:06 +0200 Subject: [PATCH] Fix false-positive compiler warning Fixes the following false-positive warning of gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 in -m32 mode: ``` /home/even/proj/proj/src/4D_api.cpp: In function 'int cs2cs_emulation_setup(PJ*)': /home/even/proj/proj/src/4D_api.cpp:949:64: warning: '%s' directive output between 9 and 2147483645 bytes may cause result to exceed 'INT_MAX' [-Wformat-truncation=] 949 | "break_cs2cs_recursion proj=helmert exact %s " | ``` --- src/4D_api.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/4D_api.cpp b/src/4D_api.cpp index ece2ef5bf5..d112a657d0 100644 --- a/src/4D_api.cpp +++ b/src/4D_api.cpp @@ -920,10 +920,8 @@ static int cs2cs_emulation_setup(PJ *P) { /* We ignore helmert if we have grid shift */ p = P->hgridshift ? nullptr : pj_param_exists(P->params, "towgs84"); while (p) { - char *def; - char *s = p->param; - double *d = P->datum_params; - size_t n = strlen(s); + const char *const s = p->param; + const double *const d = P->datum_params; /* We ignore null helmert shifts (common in auto-translated resource * files, e.g. epsg) */ @@ -938,19 +936,17 @@ static int cs2cs_emulation_setup(PJ *P) { break; } + const size_t n = strlen(s); if (n <= 8) /* 8==strlen ("towgs84=") */ return 0; - size_t def_size = 100 + n; - def = static_cast(malloc(def_size)); - if (nullptr == def) - return 0; - snprintf(def, def_size, - "break_cs2cs_recursion proj=helmert exact %s " - "convention=position_vector", - s); - Q = pj_create_internal(P->ctx, def); - free(def); + const size_t def_max_size = 100 + n; + std::string def; + def.reserve(def_max_size); + def += "break_cs2cs_recursion proj=helmert exact "; + def += s; + def += " convention=position_vector"; + Q = pj_create_internal(P->ctx, def.c_str()); if (nullptr == Q) return 0; pj_inherit_ellipsoid_def(P, Q);