Skip to content

Commit

Permalink
more checking of input parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardhartnett committed Dec 25, 2024
1 parent 1becc9e commit ae481c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/g2cinq.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,9 @@ g2c_inq_dim(int g2cid, int msg_num, int prod_num, int dim_num, size_t *len,
int d;
int ret = G2C_NOERROR;

/* Is this an open GRIB2 file? */
if (g2cid < 0 || g2cid > G2C_MAX_FILES)
/* Are these valid IDs? */
if (g2cid < 0 || g2cid > G2C_MAX_FILES || msg_num < 0 ||
prod_num < 0 || dim_num < 0)
return G2C_EBADID;

/* If using threading, lock the mutex. */
Expand Down
18 changes: 16 additions & 2 deletions tests/tst_inq.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ main()
printf("Testing g2c_inq()/g2c_inq_msg()/g2c_inq_prod() calls...\n");
{
int op;
size_t dimlen;
char dimname[G2C_MAX_NAME];

for (op = 0; op < NUM_OPEN; op++)
{
Expand Down Expand Up @@ -95,6 +97,20 @@ main()
if ((ret = g2c_inq_prod(g2cid, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL)))
return ret;

/* Won't work, bad IDs. */
if ((ret = g2c_inq_dim(-1, 0, 0, 0, &dimlen, dimname, NULL)) != G2C_EBADID)
return G2C_ERROR;
if ((ret = g2c_inq_dim(G2C_MAX_FILES + 1, 0, 0, 0, &dimlen, dimname, NULL)) != G2C_EBADID)
return G2C_ERROR;
if ((ret = g2c_inq_dim(10, 0, 0, 0, &dimlen, dimname, NULL)) != G2C_EBADID)
return G2C_ERROR;
if ((ret = g2c_inq_dim(g2cid, -1, 0, 0, &dimlen, dimname, NULL)) != G2C_EBADID)
return G2C_ERROR;
if ((ret = g2c_inq_dim(g2cid, 0, -1, 0, &dimlen, dimname, NULL)) != G2C_EBADID)
return G2C_ERROR;
if ((ret = g2c_inq_dim(g2cid, 0, 0, -1, &dimlen, dimname, NULL)) != G2C_EBADID)
return G2C_ERROR;

/* Check each message. */
for (m = 0; m < num_msg; m++)
{
Expand Down Expand Up @@ -150,8 +166,6 @@ main()
short year;
short center, subcenter;
unsigned char master_version, local_version;
size_t dimlen;
char dimname[G2C_MAX_NAME];
int p;

printf("\t\tinquiring about message %d...\n", m);
Expand Down

0 comments on commit ae481c9

Please sign in to comment.