File tree Expand file tree Collapse file tree 4 files changed +28
-1
lines changed Expand file tree Collapse file tree 4 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,10 @@ extern nchashkey_t hash_fast(const char*, size_t length);
6868
6969#define MAX (a ,b ) ((a) > (b) ? (a) : (b))
7070
71+ #ifndef SIZE_MAX
72+ #define SIZE_MAX ((size_t)-1)
73+ #endif
74+
7175/* Forward */
7276static const unsigned int NC_nprimes ;
7377static const unsigned int NC_primes [16386 ];
@@ -175,12 +179,14 @@ NC_hashmapnew(size_t startsize)
175179 if (startsize == 0 || startsize < MINTABLESIZE )
176180 startsize = MINTABLESIZE ;
177181 else {
182+ if (startsize > SIZE_MAX / 4 ){nullfree (hm );return 0 ;}
178183 startsize *= 4 ;
179184 startsize /= 3 ;
180185 startsize = findPrimeGreaterThan (startsize );
181186 if (startsize == 0 ) {nullfree (hm ); return 0 ;}
182187 }
183188 hm -> table = (NC_hentry * )calloc (sizeof (NC_hentry ), (size_t )startsize );
189+ if (hm -> table == NULL ) {nullfree (hm );return 0 ;}
184190 hm -> alloc = startsize ;
185191 hm -> active = 0 ;
186192 return hm ;
Original file line number Diff line number Diff line change @@ -88,6 +88,9 @@ new_x_NC_attr(
8888
8989 assert(!(xsz == 0 && nelems != 0));
9090
91+ if(sz > SIZE_MAX -xsz)
92+ return NULL;
93+
9194 sz += xsz;
9295
9396 attrp = (NC_attr *) malloc(sz);
Original file line number Diff line number Diff line change @@ -541,6 +541,8 @@ v1h_get_NC_dimarray(v1hs *gsp, NC_dimarray *ncap)
541541 if (type != NC_DIMENSION )
542542 return EINVAL ;
543543
544+ if (ncap -> nelems > SIZE_MAX / sizeof (NC_dim * ))
545+ return NC_ERANGE ;
544546 ncap -> value = (NC_dim * * ) calloc (1 ,ncap -> nelems * sizeof (NC_dim * ));
545547 if (ncap -> value == NULL )
546548 return NC_ENOMEM ;
@@ -1192,13 +1194,17 @@ v1h_get_NC_vararray(v1hs *gsp, NC_vararray *ncap)
11921194 /* else */
11931195 if (type != NC_VARIABLE )
11941196 return EINVAL ;
1195-
1197+
1198+ if (ncap -> nelems > SIZE_MAX / sizeof (NC_var * ))
1199+ return NC_ERANGE ;
11961200 ncap -> value = (NC_var * * ) calloc (1 ,ncap -> nelems * sizeof (NC_var * ));
11971201 if (ncap -> value == NULL )
11981202 return NC_ENOMEM ;
11991203 ncap -> nalloc = ncap -> nelems ;
12001204
12011205 ncap -> hashmap = NC_hashmapnew (ncap -> nelems );
1206+ if (ncap -> hashmap == NULL )
1207+ return NC_ENOMEM ;
12021208 {
12031209 NC_var * * vpp = ncap -> value ;
12041210 NC_var * const * const end = & vpp [ncap -> nelems ];
Original file line number Diff line number Diff line change @@ -72,13 +72,21 @@ new_x_NC_var(
7272 size_t ndims )
7373{
7474 NC_var * varp ;
75+
76+ if (ndims > SIZE_MAX / sizeof (int ))
77+ return NULL ;
7578 const size_t o1 = M_RNDUP (ndims * sizeof (int ));
79+
80+ if (ndims > SIZE_MAX / sizeof (size_t ))
81+ return NULL ;
7682 const size_t o2 = M_RNDUP (ndims * sizeof (size_t ));
7783
7884#ifdef MALLOCHACK
7985 const size_t sz = M_RNDUP (sizeof (NC_var )) +
8086 o1 + o2 + ndims * sizeof (off_t );
8187#else /*!MALLOCHACK*/
88+ if (ndims > SIZE_MAX / sizeof (off_t ))
89+ return NULL ;
8290 const size_t o3 = ndims * sizeof (off_t );
8391 const size_t sz = sizeof (NC_var );
8492#endif /*!MALLOCHACK*/
@@ -477,6 +485,8 @@ NC_var_shape(NC_var *varp, const NC_dimarray *dims)
477485 /*if(!(shp == varp->shape && IS_RECVAR(varp)))*/
478486 if ( shp != NULL && (shp != varp -> shape || !IS_RECVAR (varp )))
479487 {
488+ if (product <= 0 )
489+ return NC_ERANGE ;
480490 if ( ((off_t )(* shp )) <= OFF_T_MAX / product )
481491 {
482492 product *= (* shp > 0 ? (off_t )* shp : 1 );
@@ -525,6 +535,8 @@ NC_check_vlen(NC_var *varp, long long vlen_max) {
525535 for (ii = IS_RECVAR (varp ) ? 1 : 0 ; ii < varp -> ndims ; ii ++ ) {
526536 if (!varp -> shape )
527537 return 0 ; /* Shape is undefined/NULL. */
538+ if (prod <= 0 )
539+ return 0 ; /* Multiplication operations may result in overflow */
528540 if ((long long )varp -> shape [ii ] > vlen_max / prod ) {
529541 return 0 ; /* size in bytes won't fit in a 32-bit int */
530542 }
You can’t perform that action at this time.
0 commit comments