From 7c28714fa2f9f4bdfa2733ea8c125aae06737b30 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 11 Nov 2024 13:54:28 +0100 Subject: [PATCH] HDF4: fix REMQUOTE implementation that caused valgrind to warn about overlapping source and target buffers --- frmts/hdf4/hdf-eos/GDapi.c | 14 +++++++++----- frmts/hdf4/hdf-eos/SWapi.c | 28 ++++++++++++++++------------ 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/frmts/hdf4/hdf-eos/GDapi.c b/frmts/hdf4/hdf-eos/GDapi.c index 1475ee0fee38..1a8be985d47b 100644 --- a/frmts/hdf4/hdf-eos/GDapi.c +++ b/frmts/hdf4/hdf-eos/GDapi.c @@ -4355,10 +4355,14 @@ GDinqattrs(int32 gridID, char *attrnames, int32 * strbufsize) -#define REMQUOTE \ -\ -memmove(utlstr, utlstr + 1, strlen(utlstr) - 2); \ -utlstr[strlen(utlstr) - 2] = 0; +#define REMQUOTE(x) do { \ + char* l_x = x; \ + const size_t l_x_len = strlen(l_x); \ + if (l_x_len >= 2 && l_x[0] == '"' && l_x[l_x_len - 1] == '"') {\ + memmove(l_x, l_x + 1, l_x_len - 2); \ + l_x[l_x_len - 2] = 0; \ + } \ + } while(0) /*----------------------------------------------------------------------------| @@ -4648,7 +4652,7 @@ GDinqfields(int32 gridID, char *fieldlist, int32 rank[], /* Strip off double quotes */ /* ----------------------- */ - REMQUOTE + REMQUOTE(utlstr); /* Add to fieldlist */ diff --git a/frmts/hdf4/hdf-eos/SWapi.c b/frmts/hdf4/hdf-eos/SWapi.c index 1ebe9ba595bb..4ceb690b3cce 100644 --- a/frmts/hdf4/hdf-eos/SWapi.c +++ b/frmts/hdf4/hdf-eos/SWapi.c @@ -3286,10 +3286,14 @@ SWinqattrs(int32 swathID, char *attrnames, int32 * strbufsize) return (nattr); } -#define REMQUOTE \ -\ -memmove(utlstr, utlstr + 1, strlen(utlstr) - 2); \ -utlstr[strlen(utlstr) - 2] = 0; +#define REMQUOTE(x) do { \ + char* l_x = x; \ + const size_t l_x_len = strlen(l_x); \ + if (l_x_len >= 2 && l_x[0] == '"' && l_x[l_x_len - 1] == '"') {\ + memmove(l_x, l_x + 1, l_x_len - 2); \ + l_x[l_x_len - 2] = 0; \ + } \ + } while(0) /*----------------------------------------------------------------------------| @@ -3408,7 +3412,7 @@ SWinqdims(int32 swathID, char *dimnames, int32 dims[]) /* Strip off double quotes */ /* ----------------------- */ - REMQUOTE + REMQUOTE(utlstr); /* If not first name then add comma delimiter */ if (nDim > 0) @@ -3556,8 +3560,8 @@ SWinqmaps(int32 swathID, char *dimmaps, int32 offset[], int32 increment[]) { /* Get Geo Dim, remove quotes, add "/" */ EHgetmetavalue(metaptrs, "GeoDimension", utlstr); - REMQUOTE - strcat(utlstr, "/"); + REMQUOTE(utlstr); + strcat(utlstr, "/"); /* If not first map then add comma delimiter. */ if (nMap > 0) @@ -3570,7 +3574,7 @@ SWinqmaps(int32 swathID, char *dimmaps, int32 offset[], int32 increment[]) /* Get Data Dim, remove quotes */ EHgetmetavalue(metaptrs, "DataDimension", utlstr); - REMQUOTE + REMQUOTE(utlstr); /* Add to map list */ strcat(dimmaps, utlstr); @@ -3722,8 +3726,8 @@ SWinqidxmaps(int32 swathID, char *idxmaps, int32 idxsizes[]) { /* Get Geo Dim, remove quotes, add "/" */ EHgetmetavalue(metaptrs, "GeoDimension", utlstr); - REMQUOTE - strcat(utlstr, "/"); + REMQUOTE(utlstr); + strcat(utlstr, "/"); /* If not first map then add comma delimiter. */ if (nMap > 0) @@ -3747,7 +3751,7 @@ SWinqidxmaps(int32 swathID, char *idxmaps, int32 idxsizes[]) /* Get Data Dim, remove quotes */ EHgetmetavalue(metaptrs, "DataDimension", utlstr); - REMQUOTE + REMQUOTE(utlstr); /* Add to map list */ strcat(idxmaps, utlstr); @@ -3935,7 +3939,7 @@ SWinqfields(int32 swathID, const char *fieldtype, char *fieldlist, int32 rank[], /* Strip off double quotes */ /* ----------------------- */ - REMQUOTE + REMQUOTE(utlstr); /* Add to fieldlist */