Skip to content

Commit 81af364

Browse files
authored
Merge pull request OSGeo#13604 from rouault/seek_vsi_l_offset
Avoid accidental use of non-long-offset for VSIFSeekL()
2 parents bb734e3 + 8eec1c9 commit 81af364

File tree

84 files changed

+545
-290
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+545
-290
lines changed

autotest/cpp/test_c_include_from_cpp_file.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
* SPDX-License-Identifier: MIT
1010
****************************************************************************/
1111

12-
extern "C"
13-
{
1412
#include "cpl_atomic_ops.h"
1513
#include "cpl_conv.h"
1614
#include "cpl_csv.h"
@@ -28,7 +26,6 @@ extern "C"
2826
#include "ogr_api.h"
2927
#include "ogr_core.h"
3028
#include "ogr_srs_api.h"
31-
}
3229

3330
int main()
3431
{

frmts/aaigrid/aaigriddataset.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,8 @@ GDALDataset *AAIGDataset::CommonOpen(GDALOpenInfo *poOpenInfo,
11781178
}
11791179
(pabyChunk.get())[nChunkSize] = '\0';
11801180

1181-
if (VSIFSeekL(poDS->fp, nStartOfData, SEEK_SET) < 0)
1181+
if (VSIFSeekL(poDS->fp, static_cast<vsi_l_offset>(nStartOfData),
1182+
SEEK_SET) < 0)
11821183
{
11831184
return nullptr;
11841185
}

frmts/airsar/airsardataset.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class AirSARDataset final : public GDALPamDataset
4444

4545
CPLErr LoadLine(int iLine);
4646

47-
static char **ReadHeader(VSILFILE *fp, int nFileOffset,
47+
static char **ReadHeader(VSILFILE *fp, vsi_l_offset nFileOffset,
4848
const char *pszPrefix, int nMaxLines);
4949

5050
public:
@@ -352,7 +352,7 @@ CPLErr AirSARDataset::LoadLine(int iLine)
352352
/* blank record or some zero bytes. */
353353
/************************************************************************/
354354

355-
char **AirSARDataset::ReadHeader(VSILFILE *fp, int nFileOffset,
355+
char **AirSARDataset::ReadHeader(VSILFILE *fp, vsi_l_offset nFileOffset,
356356
const char *pszPrefix, int nMaxLines)
357357

358358
{
@@ -561,13 +561,14 @@ GDALDataset *AirSARDataset::Open(GDALOpenInfo *poOpenInfo)
561561
/* Read and merge parameter header into metadata. Prefix */
562562
/* parameter header values with PH_. */
563563
/* -------------------------------------------------------------------- */
564-
int nPHOffset = 0;
564+
vsi_l_offset nPHOffset = 0;
565565

566566
if (CSLFetchNameValue(papszMD, "MH_BYTE_OFFSET_OF_PARAMETER_HEADER") !=
567567
nullptr)
568568
{
569-
nPHOffset = atoi(
570-
CSLFetchNameValue(papszMD, "MH_BYTE_OFFSET_OF_PARAMETER_HEADER"));
569+
nPHOffset = std::strtoull(
570+
CSLFetchNameValue(papszMD, "MH_BYTE_OFFSET_OF_PARAMETER_HEADER"),
571+
nullptr, 0);
571572
char **papszPHInfo = ReadHeader(poDS->fp, nPHOffset, "PH", 100);
572573

573574
papszMD = CSLInsertStrings(papszMD, CSLCount(papszMD), papszPHInfo);

frmts/bmp/bmpdataset.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,10 @@ CPLErr BMPRasterBand::SetColorTable(GDALColorTable *poColorTable)
633633
(GByte)oEntry.c3; // Blue
634634
}
635635

636-
VSIFSeekL(poGDS->fp, BFH_SIZE + poGDS->sInfoHeader.iSize, SEEK_SET);
636+
VSIFSeekL(
637+
poGDS->fp,
638+
static_cast<vsi_l_offset>(BFH_SIZE + poGDS->sInfoHeader.iSize),
639+
SEEK_SET);
637640
if (VSIFWriteL(poGDS->pabyColorTable, 1,
638641
static_cast<size_t>(poGDS->nColorElems) *
639642
poGDS->sInfoHeader.iClrUsed,
@@ -754,7 +757,9 @@ BMPComprRasterBand::BMPComprRasterBand(BMPDataset *poDSIn, int nBandIn)
754757
return;
755758
}
756759

757-
if (VSIFSeekL(poDSIn->fp, poDSIn->sFileHeader.iOffBits, SEEK_SET) != 0 ||
760+
if (VSIFSeekL(poDSIn->fp,
761+
static_cast<vsi_l_offset>(poDSIn->sFileHeader.iOffBits),
762+
SEEK_SET) != 0 ||
758763
VSIFReadL(pabyComprBuf, 1, iComprSize, poDSIn->fp) < iComprSize)
759764
{
760765
CPLError(CE_Failure, CPLE_FileIO,
@@ -1146,7 +1151,7 @@ GDALDataset *BMPDataset::Open(GDALOpenInfo *poOpenInfo)
11461151
poDS->fp = poOpenInfo->fpL;
11471152
poOpenInfo->fpL = nullptr;
11481153

1149-
VSIFSeekL(poDS->fp, BFH_SIZE, SEEK_SET);
1154+
VSIFSeekL(poDS->fp, BFH_SIZE + 0, SEEK_SET);
11501155
VSIFReadL(&poDS->sInfoHeader.iSize, 1, 4, poDS->fp);
11511156
#ifdef CPL_MSB
11521157
CPL_SWAP32PTR(&poDS->sInfoHeader.iSize);

frmts/ceos2/sar_ceosdataset.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "ogr_srs_api.h"
2020

2121
#include <algorithm>
22+
#include <cinttypes>
2223

2324
static GInt16 CastToGInt16(float val)
2425
{
@@ -226,10 +227,11 @@ CPLErr SAR_CEOSRasterBand::IReadBlock(int /* nBlockXOff */, int nBlockYOff,
226227

227228
struct CeosSARImageDesc *ImageDesc = &(poGDS->sVolume.ImageDesc);
228229

229-
int offset;
230+
int offsetStart = 0;
230231
CalcCeosSARImageFilePosition(&(poGDS->sVolume), nBand, nBlockYOff + 1,
231-
nullptr, &offset);
232+
nullptr, &offsetStart);
232233

234+
vsi_l_offset offset = offsetStart;
233235
offset += ImageDesc->ImageDataStart;
234236

235237
/* -------------------------------------------------------------------- */
@@ -361,9 +363,10 @@ CPLErr CCPRasterBand::IReadBlock(CPL_UNUSED int nBlockXOff, int nBlockYOff,
361363

362364
struct CeosSARImageDesc *ImageDesc = &(poGDS->sVolume.ImageDesc);
363365

364-
int offset = ImageDesc->FileDescriptorLength +
365-
ImageDesc->BytesPerRecord * nBlockYOff +
366-
ImageDesc->ImageDataStart;
366+
const vsi_l_offset offset =
367+
ImageDesc->FileDescriptorLength +
368+
static_cast<vsi_l_offset>(ImageDesc->BytesPerRecord) * nBlockYOff +
369+
ImageDesc->ImageDataStart;
367370

368371
/* -------------------------------------------------------------------- */
369372
/* Load all the pixel data associated with this scanline. */
@@ -377,9 +380,11 @@ CPLErr CCPRasterBand::IReadBlock(CPL_UNUSED int nBlockXOff, int nBlockYOff,
377380
nBytesToRead)
378381
{
379382
CPLError(CE_Failure, CPLE_FileIO,
380-
"Error reading %d bytes of CEOS record data at offset %d.\n"
383+
"Error reading %d bytes of CEOS record data at offset %" PRIu64
384+
".\n"
381385
"Reading file %s failed.",
382-
nBytesToRead, offset, poGDS->GetDescription());
386+
nBytesToRead, static_cast<uint64_t>(offset),
387+
poGDS->GetDescription());
383388
CPLFree(pabyRecord);
384389
return CE_Failure;
385390
}
@@ -502,9 +507,10 @@ CPLErr PALSARRasterBand::IReadBlock(int /* nBlockXOff */, int nBlockYOff,
502507

503508
struct CeosSARImageDesc *ImageDesc = &(poGDS->sVolume.ImageDesc);
504509

505-
int offset = ImageDesc->FileDescriptorLength +
506-
ImageDesc->BytesPerRecord * nBlockYOff +
507-
ImageDesc->ImageDataStart;
510+
const vsi_l_offset offset =
511+
ImageDesc->FileDescriptorLength +
512+
static_cast<vsi_l_offset>(ImageDesc->BytesPerRecord) * nBlockYOff +
513+
ImageDesc->ImageDataStart;
508514

509515
/* -------------------------------------------------------------------- */
510516
/* Load all the pixel data associated with this scanline. */
@@ -518,9 +524,11 @@ CPLErr PALSARRasterBand::IReadBlock(int /* nBlockXOff */, int nBlockYOff,
518524
nBytesToRead)
519525
{
520526
CPLError(CE_Failure, CPLE_FileIO,
521-
"Error reading %d bytes of CEOS record data at offset %d.\n"
527+
"Error reading %d bytes of CEOS record data at offset %" PRIu64
528+
".\n"
522529
"Reading file %s failed.",
523-
nBytesToRead, offset, poGDS->GetDescription());
530+
nBytesToRead, static_cast<uint64_t>(offset),
531+
poGDS->GetDescription());
524532
CPLFree(pabyRecord);
525533
return CE_Failure;
526534
}
@@ -1712,7 +1720,8 @@ void SAR_CEOSDataset::ScanForGCPs()
17121720
&nFileOffset);
17131721

17141722
GInt32 anRecord[192 / 4];
1715-
if (VSIFSeekL(fpImage, nFileOffset, SEEK_SET) != 0 ||
1723+
if (VSIFSeekL(fpImage, static_cast<vsi_l_offset>(nFileOffset),
1724+
SEEK_SET) != 0 ||
17161725
VSIFReadL(anRecord, 1, 192, fpImage) != 192)
17171726
break;
17181727

@@ -2169,7 +2178,7 @@ static int ProcessData(VSILFILE *fp, int fileid, CeosSARVolume_t *sar,
21692178
{
21702179
unsigned char temp_buffer[CEOS_HEADER_LENGTH];
21712180
unsigned char *temp_body = nullptr;
2172-
int start = 0;
2181+
vsi_l_offset start = 0;
21732182
int CurrentBodyLength = 0;
21742183
int CurrentType = 0;
21752184
int CurrentSequence = 0;

frmts/cosar/cosar_dataset.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
// Commented out the unused defines.
2020
// #define BIB_OFFSET 0 /* Bytes in burst, valid only for ScanSAR */
2121
// #define RSRI_OFFSET 4 /* Range Sample Relative Index */
22-
constexpr int RS_OFFSET = 8; /* Range Samples, the length of a range line */
22+
constexpr vsi_l_offset RS_OFFSET =
23+
8; /* Range Samples, the length of a range line */
2324
// #define AS_OFFSET 12 /* Azimuth Samples, the length of an azimuth column
2425
// */ #define BI_OFFSET 16 /* Burst Index, the index number of the burst */
25-
constexpr int RTNB_OFFSET =
26+
constexpr vsi_l_offset RTNB_OFFSET =
2627
20; /* Rangeline total number of bytes, incl. annot. */
2728
// #define TNL_OFFSET 24 /* Total Number of Lines */
2829
constexpr int MAGIC1_OFFSET = 28; /* Magic number 1: 0x43534152 */

frmts/ecw/gdal_ecw.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class VSIIOStream final : public CNCSJPCIOStream
196196
lengthOfJPData = size;
197197
bWritable = bWrite;
198198
bSeekable = bSeekableIn;
199-
VSIFSeekL(fpVSIL, startOfJPData, SEEK_SET);
199+
VSIFSeekL(fpVSIL, static_cast<vsi_l_offset>(startOfJPData), SEEK_SET);
200200
m_Filename = CPLStrdup(pszFilename);
201201

202202
#if ECWSDK_VERSION >= 55
@@ -268,16 +268,22 @@ class VSIIOStream final : public CNCSJPCIOStream
268268
switch (origin)
269269
{
270270
case START:
271-
success =
272-
(0 == VSIFSeekL(fpVSIL, offset + startOfJPData, SEEK_SET));
271+
success = (0 == VSIFSeekL(fpVSIL,
272+
static_cast<vsi_l_offset>(offset) +
273+
startOfJPData,
274+
SEEK_SET));
273275
break;
274276

275277
case CURRENT:
276-
success = (0 == VSIFSeekL(fpVSIL, offset, SEEK_CUR));
278+
success =
279+
(0 == VSIFSeekL(fpVSIL, static_cast<vsi_l_offset>(offset),
280+
SEEK_CUR));
277281
break;
278282

279283
case END:
280-
success = (0 == VSIFSeekL(fpVSIL, offset, SEEK_END));
284+
success =
285+
(0 == VSIFSeekL(fpVSIL, static_cast<vsi_l_offset>(offset),
286+
SEEK_END));
281287
break;
282288
}
283289
if (!success)

frmts/georaster/georaster_dataset.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,12 +762,12 @@ boolean GeoRasterDataset::JPEG_CopyDirect(const char *pszJPGFilename,
762762

763763
void *pBuffer = (GByte *)VSIMalloc(sizeof(GByte) * nCache);
764764

765-
VSIFSeekL(fpInput, 0L, SEEK_END);
765+
VSIFSeekL(fpInput, 0, SEEK_END);
766766

767767
size_t nCount = 0;
768768
const size_t nDataLength = VSIFTellL(fpInput);
769769

770-
VSIFSeekL(fpInput, 0L, SEEK_SET);
770+
VSIFSeekL(fpInput, 0, SEEK_SET);
771771

772772
GUIntBig nCurOff = 0;
773773

frmts/gff/gff_dataset.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ CPLErr GFFRasterBand::IReadBlock(int /* nBlockXOff */, int nBlockYOff,
135135
long nOffset = poGDS->nLength;
136136

137137
VSIFSeekL(poGDS->fp,
138-
nOffset + (poGDS->GetRasterXSize() * nBlockYOff * (nSampleSize)),
138+
nOffset + (static_cast<vsi_l_offset>(poGDS->GetRasterXSize()) *
139+
nBlockYOff * (nSampleSize)),
139140
SEEK_SET);
140141

141142
/* Ingest entire range line */

frmts/grib/degrib/degrib/degrib2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ int FindGRIBMsg (VSILFILE *fp, int msgNum, sInt4 *offset, int *curMsg)
352352
} else {
353353
jump = gribLen - 16;
354354
}
355-
VSIFSeekL(fp, jump, SEEK_CUR);
355+
VSIFSeekL(fp, static_cast<vsi_l_offset>(jump), SEEK_CUR);
356356
*offset = *offset + gribLen + buffLen;
357357
cnt++;
358358
}

0 commit comments

Comments
 (0)