Commit 97995d73 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

cgribex: check gridsize int limit.

parent 84bd6fe8
......@@ -10,6 +10,7 @@
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <sys/types.h>
......@@ -479,6 +480,13 @@ void cdiDefTableID(int tableID);
void gridGenXvals(int xsize, double xfirst, double xlast, double xinc, double *xvals);
void gridGenYvals(int gridtype, int ysize, double yfirst, double ylast, double yinc, double *yvals);
static inline
void cdi_check_gridsize_int_limit(const char *format, size_t gridsize)
{
if ( gridsize > INT_MAX ) Error("%s format grid size (%zu) limit exceeded (%zu)!", format, gridsize, INT_MAX);
}
#if defined (__cplusplus)
}
#endif
......
#ifndef EXSE_H
#define EXSE_H
#include <limits.h>
enum {
EXSE_SINGLE_PRECISION = 4,
EXSE_DOUBLE_PRECISION = 8,
};
static inline
void exse_check_gridsize(const char *format, size_t gridsize)
{
if ( gridsize > INT_MAX ) Error("%s format grid size (%zu) limit exceeded (%zu)!", format, gridsize, INT_MAX);
}
#endif
......@@ -32,7 +32,7 @@ size_t grbEncode(int filetype, int memtype, int varID, int levelID, int vlistID,
nbytes = cgribexEncode(memtype, varID, levelID, vlistID, gridID, zaxisID,
date, time, tsteptype, numavg,
(long) datasize, data, nmiss, *gribbuffer, gribbuffersize);
datasize, data, nmiss, *gribbuffer, gribbuffersize);
}
else
#endif
......
......@@ -165,14 +165,18 @@ void cgribexGetGrid(stream_t *streamptr, int *isec2, int *isec4, grid_t *grid, i
{
bool ijDirectionIncrementGiven = gribbyte_get_bit(ISEC2_ResFlag, 1);
bool uvRelativeToGrid = gribbyte_get_bit(ISEC2_ResFlag, 5);
if ( uvRelativeToGrid ) grid->uvRelativeToGrid = 1;
if ( ISEC4_NumValues != ISEC2_NumLon*ISEC2_NumLat )
Error("numberOfPoints (%d) and gridSize (%d) differ!", ISEC4_NumValues, ISEC2_NumLon*ISEC2_NumLat);
grid->size = ISEC4_NumValues;
grid->x.size = ISEC2_NumLon;
grid->y.size = ISEC2_NumLat;
size_t nvalues = (size_t) ISEC4_NumValues;
size_t nlon = (size_t) ISEC2_NumLon;
size_t nlat = (size_t) ISEC2_NumLat;
if ( nvalues != nlon*nlat )
Error("numberOfPoints (%zu) and gridSize (%zu) differ!", nvalues, nlon*nlat);
grid->size = nvalues;
grid->x.size = nlon;
grid->y.size = nlat;
if ( gridtype == GRID_GAUSSIAN ) grid->np = ISEC2_NumPar;
grid->x.inc = 0;
grid->y.inc = 0;
......@@ -241,10 +245,10 @@ void cgribexGetGrid(stream_t *streamptr, int *isec2, int *isec4, grid_t *grid, i
bool uvRelativeToGrid = gribbyte_get_bit(ISEC2_ResFlag, 5);
if ( uvRelativeToGrid ) grid->uvRelativeToGrid = 1;
grid->np = ISEC2_NumPar;
grid->size = ISEC4_NumValues;
grid->size = (size_t)ISEC4_NumValues;
grid->rowlon = ISEC2_RowLonPtr;
grid->nrowlon = ISEC2_NumLat;
grid->y.size = ISEC2_NumLat;
grid->nrowlon = (size_t)ISEC2_NumLat;
grid->y.size = (size_t)ISEC2_NumLat;
grid->x.inc = 0;
grid->y.inc = 0;
grid->x.flag = 0;
......@@ -283,12 +287,15 @@ void cgribexGetGrid(stream_t *streamptr, int *isec2, int *isec4, grid_t *grid, i
bool uvRelativeToGrid = gribbyte_get_bit(ISEC2_ResFlag, 5);
if ( uvRelativeToGrid ) grid->uvRelativeToGrid = 1;
if ( ISEC4_NumValues != ISEC2_NumLon*ISEC2_NumLat )
Error("numberOfPoints (%d) and gridSize (%d) differ!", ISEC4_NumValues, ISEC2_NumLon*ISEC2_NumLat);
size_t nvalues = (size_t) ISEC4_NumValues;
size_t nlon = (size_t) ISEC2_NumLon;
size_t nlat = (size_t) ISEC2_NumLat;
if ( nvalues != nlon*nlat )
Error("numberOfPoints (%zu) and gridSize (%zu) differ!", nvalues, nlon*nlat);
grid->size = ISEC4_NumValues;
grid->x.size = ISEC2_NumLon;
grid->y.size = ISEC2_NumLat;
grid->size = nvalues;
grid->x.size = nlon;
grid->y.size = nlat;
grid->x.first = 0;
grid->x.last = 0;
......@@ -301,16 +308,13 @@ void cgribexGetGrid(stream_t *streamptr, int *isec2, int *isec4, grid_t *grid, i
}
else if ( gridtype == GRID_SPECTRAL )
{
grid->size = ISEC4_NumValues;
grid->size = (size_t) ISEC4_NumValues;
grid->trunc = ISEC2_PentaJ;
if ( ISEC2_RepMode == 2 )
grid->lcomplex = 1;
else
grid->lcomplex = 0;
}
grid->lcomplex = (ISEC2_RepMode == 2) ? 1 : 0;
}
else if ( gridtype == GRID_GME )
{
grid->size = ISEC4_NumValues;
grid->size = (size_t) ISEC4_NumValues;
grid->gme.nd = ISEC2_GME_ND;
grid->gme.ni = ISEC2_GME_NI;
grid->gme.ni2 = ISEC2_GME_NI2;
......@@ -318,7 +322,7 @@ void cgribexGetGrid(stream_t *streamptr, int *isec2, int *isec4, grid_t *grid, i
}
else if ( gridtype == GRID_GENERIC )
{
grid->size = ISEC4_NumValues;
grid->size = (size_t) ISEC4_NumValues;
grid->x.size = 0;
grid->y.size = 0;
}
......@@ -1614,9 +1618,9 @@ void cgribexDefGrid(int *isec1, int *isec2, double *fsec2, int *isec4, int gridI
if ( gridtype == GRID_GENERIC )
{
int gridsize = gridInqSize(gridID);
int xsize = gridInqXsize(gridID);
int ysize = gridInqYsize(gridID);
int gridsize = (int)gridInqSize(gridID);
int xsize = (int)gridInqXsize(gridID);
int ysize = (int)gridInqYsize(gridID);
if ( (ysize == 32 || ysize == 48 || ysize == 64 ||
ysize == 96 || ysize == 160 || ysize == 192 ||
......@@ -1692,8 +1696,8 @@ void cgribexDefGrid(int *isec1, int *isec2, double *fsec2, int *isec4, int gridI
else
ISEC2_GridType = GRIB1_GTYPE_LATLON;
int nlon = gridInqXsize(gridID);
int nlat = gridInqYsize(gridID);
int nlon = (int)gridInqXsize(gridID);
int nlat = (int)gridInqYsize(gridID);
if ( gridtype == GRID_GAUSSIAN_REDUCED )
{
......@@ -1777,8 +1781,8 @@ void cgribexDefGrid(int *isec1, int *isec2, double *fsec2, int *isec4, int gridI
}
case GRID_LCC:
{
int xsize = gridInqXsize(gridID);
int ysize = gridInqYsize(gridID);
int xsize = (int)gridInqXsize(gridID);
int ysize = (int)gridInqYsize(gridID);
double lon_0, lat_0, lat_1, lat_2, a, rf, xval_0, yval_0, x_0, y_0;
gridInqParamLCC(gridID, grid_missval, &lon_0, &lat_0, &lat_1, &lat_2, &a, &rf, &xval_0, &yval_0, &x_0, &y_0);
......@@ -2078,7 +2082,7 @@ void cgribexDefEnsembleVar(int *isec1, int vlistID, int varID)
#if defined (HAVE_LIBCGRIBEX)
size_t cgribexEncode(int memtype, int varID, int levelID, int vlistID, int gridID, int zaxisID,
int vdate, int vtime, int tsteptype, int numavg,
long datasize, const void *data, int nmiss, void *gribbuffer, size_t gribbuffersize)
size_t datasize, const void *data, int nmiss, void *gribbuffer, size_t gribbuffersize)
{
int iret = 0, iword = 0;
int isec0[2], isec1[4096], isec2[4096], isec3[2], isec4[512];
......@@ -2108,7 +2112,9 @@ size_t cgribexEncode(int memtype, int varID, int levelID, int vlistID, int gridI
cgribexDefEnsembleVar(isec1, vlistID, varID);
ISEC4_NumValues = gridInqSize(gridID);
cdi_check_gridsize_int_limit("GRIB1", datasize);
ISEC4_NumValues = (int) datasize;
ISEC4_NumBits = grbBitsPerValue(datatype);
if ( nmiss > 0 )
......
#ifndef _STREAM_CGRIBEX_H
#define _STREAM_CGRIBEX_H
#ifndef STREAM_CGRIBEX_H
#define STREAM_CGRIBEX_H
int cgribexScanTimestep1(stream_t * streamptr);
int cgribexScanTimestep2(stream_t * streamptr);
......@@ -10,14 +10,14 @@ int cgribexDecode(int memtype, void *gribbuffer, int gribsize, void *data, long
size_t cgribexEncode(int memtype, int varID, int levelID, int vlistID, int gridID, int zaxisID,
int vdate, int vtime, int tsteptype, int numavg,
long datasize, const void *data, int nmiss, void *gribbuffer, size_t gribbuffersize);
size_t datasize, const void *data, int nmiss, void *gribbuffer, size_t gribbuffersize);
void *cgribex_handle_new_from_meassage(void *gribbuffer, size_t recsize);
void cgribex_handle_delete(void *gh);
void cgribexChangeParameterIdentification(void *gh, int code, int ltype, int lev);
#endif /* _STREAM_CGRIBEX_H */
#endif /* STREAM_CGRIBEX_H */
/*
* Local Variables:
* c-file-style: "Java"
......
......@@ -150,7 +150,7 @@ void extDefRecord(stream_t *streamptr)
header[1] = pnum;
header[2] = streamptr->record->level;
int gridID = streamptr->record->gridID;
exse_check_gridsize("EXTRA", gridInqSize(gridID));
cdi_check_gridsize_int_limit("EXTRA", gridInqSize(gridID));
header[3] = (int) gridInqSize(gridID);
extrec_t *extp = (extrec_t*) streamptr->record->exsep;
......@@ -714,7 +714,7 @@ void extWriteVarSliceDP(stream_t *streamptr, int varID, int levID, const double
header[1] = pnum;
header[2] = (int)(zaxisInqLevel(vlistInqVarZaxis(vlistID, varID), levID));
int gridID = vlistInqVarGrid(vlistID, varID);
exse_check_gridsize("EXTRA", gridInqSize(gridID));
cdi_check_gridsize_int_limit("EXTRA", gridInqSize(gridID));
header[3] = (int) gridInqSize(gridID);
extrec_t *extp = (extrec_t*) streamptr->record->exsep;
......
......@@ -246,7 +246,7 @@ void iegDefGrid(int *gdb, int gridID)
size_t xsize = gridInqXsize(gridID);
size_t ysize = gridInqYsize(gridID);
exse_check_gridsize("IEG", xsize*ysize);
cdi_check_gridsize_int_limit("IEG", xsize*ysize);
if ( gridtype == GRID_GENERIC )
{
......
......@@ -148,7 +148,7 @@ void srvDefRecord(stream_t *streamptr)
if ( gridInqSize(gridID) != xsize*ysize )
Error("Internal problem with gridsize!");
exse_check_gridsize("SERVICE", gridInqSize(gridID));
cdi_check_gridsize_int_limit("SERVICE", gridInqSize(gridID));
header[4] = (int)xsize;
header[5] = (int)ysize;
......@@ -711,7 +711,7 @@ void srvWriteVarSliceDP(stream_t *streamptr, int varID, int levID, const double
if ( gridInqSize(gridID) != xsize*ysize )
Error("Internal problem with gridsize!");
exse_check_gridsize("SERVICE", gridInqSize(gridID));
cdi_check_gridsize_int_limit("SERVICE", gridInqSize(gridID));
header[4] = xsize;
header[5] = ysize;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment