From d9a655efce161398e3250a2bad74e7648e00655d Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Sat, 24 Aug 2019 12:16:41 +0200 Subject: [PATCH 1/2] Avoid truncation or bad formatting in message header --- src/algorithms/PVT/libs/rtcm.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/algorithms/PVT/libs/rtcm.h b/src/algorithms/PVT/libs/rtcm.h index 0cbba9d07..794ca2126 100644 --- a/src/algorithms/PVT/libs/rtcm.h +++ b/src/algorithms/PVT/libs/rtcm.h @@ -594,11 +594,7 @@ private: inline void encode_header() { char header[header_length + 1] = ""; - int num_chars = std::snprintf(header, header_length + 1, "GS%4d", std::min(static_cast(body_length_), static_cast(max_body_length))); - if ((num_chars <= 0) or (num_chars > header_length)) - { - // avoid Wformat-truncation warning - } + std::snprintf(header, header_length + 1, "GS%4d", std::max(std::min(static_cast(body_length_), static_cast(max_body_length)), 0)); std::memcpy(data_, header, header_length); } From d36e9e431c072c06fcb850355dad921b51e2d780 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Sat, 24 Aug 2019 12:17:53 +0200 Subject: [PATCH 2/2] Avoid string truncation --- src/algorithms/libs/rtklib/rtklib_ionex.cc | 2 +- src/algorithms/libs/rtklib/rtklib_preceph.cc | 4 ++-- src/algorithms/libs/rtklib/rtklib_rtkcmn.cc | 2 +- src/algorithms/libs/rtklib/rtklib_sbas.cc | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/algorithms/libs/rtklib/rtklib_ionex.cc b/src/algorithms/libs/rtklib/rtklib_ionex.cc index 72f1528ff..94d641c13 100644 --- a/src/algorithms/libs/rtklib/rtklib_ionex.cc +++ b/src/algorithms/libs/rtklib/rtklib_ionex.cc @@ -463,7 +463,7 @@ void readtec(const char *file, nav_t *nav, int opt) } for (i = 0; i < MAXEXFILE; i++) { - if (!(efiles[i] = static_cast(malloc(1024)))) + if (!(efiles[i] = static_cast(malloc(MAXSTRPATH + 255)))) { for (i--; i >= 0; i--) { diff --git a/src/algorithms/libs/rtklib/rtklib_preceph.cc b/src/algorithms/libs/rtklib/rtklib_preceph.cc index 0b80640b7..bd26944b1 100644 --- a/src/algorithms/libs/rtklib/rtklib_preceph.cc +++ b/src/algorithms/libs/rtklib/rtklib_preceph.cc @@ -420,7 +420,7 @@ void readsp3(const char *file, nav_t *nav, int opt) for (i = 0; i < MAXEXFILE; i++) { - if (!(efiles[i] = static_cast(malloc(1024)))) + if (!(efiles[i] = static_cast(malloc(MAXSTRPATH + 255)))) { for (i--; i >= 0; i--) { @@ -601,7 +601,7 @@ int readdcb(const char *file, nav_t *nav, const sta_t *sta) } for (i = 0; i < MAXEXFILE; i++) { - if (!(efiles[i] = static_cast(malloc(1024)))) + if (!(efiles[i] = static_cast(malloc(MAXSTRPATH + 255)))) { for (i--; i >= 0; i--) { diff --git a/src/algorithms/libs/rtklib/rtklib_rtkcmn.cc b/src/algorithms/libs/rtklib/rtklib_rtkcmn.cc index efec6edeb..29b9d024e 100644 --- a/src/algorithms/libs/rtklib/rtklib_rtkcmn.cc +++ b/src/algorithms/libs/rtklib/rtklib_rtkcmn.cc @@ -5278,7 +5278,7 @@ int expath(const char *path, char *paths[], int nmax) } if (p && n < nmax) { - std::snprintf(paths[n++], MAXSTRPATH, "%s%s", dir, d->d_name); + std::snprintf(paths[n++], MAXSTRPATH + 255, "%s%s", dir, d->d_name); } } closedir(dp); diff --git a/src/algorithms/libs/rtklib/rtklib_sbas.cc b/src/algorithms/libs/rtklib/rtklib_sbas.cc index 3c239161e..7379a8fa5 100644 --- a/src/algorithms/libs/rtklib/rtklib_sbas.cc +++ b/src/algorithms/libs/rtklib/rtklib_sbas.cc @@ -782,7 +782,7 @@ int sbsreadmsgt(const char *file, int sel, gtime_t ts, gtime_t te, for (i = 0; i < MAXEXFILE; i++) { - if (!(efiles[i] = static_cast(malloc(1024)))) + if (!(efiles[i] = static_cast(malloc(MAXSTRPATH + 255)))) { for (i--; i >= 0; i--) {