@@ -753,7 +753,7 @@ private int
753
753
apprentice_load (struct magic_set * ms , struct magic * * magicp , uint32_t * nmagicp ,
754
754
const char * fn , int action )
755
755
{
756
- int errs = 0 ;
756
+ int errs = 0 , mflen = 0 ;
757
757
struct magic_entry * marray ;
758
758
uint32_t marraycount , i , mentrycount = 0 , starttest ;
759
759
size_t files = 0 , maxfiles = 0 ;
@@ -782,7 +782,7 @@ apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
782
782
goto out ;
783
783
}
784
784
while ((d = readdir (dir )) != NULL ) {
785
- if (snprintf (mfn , sizeof (mfn ), "%s/%s" , fn , d -> d_name ) < 0 ) {
785
+ if (( mflen = snprintf (mfn , sizeof (mfn ), "%s/%s" , fn , d -> d_name ) ) < 0 ) {
786
786
file_oomem (ms ,
787
787
strlen (fn ) + strlen (d -> d_name ) + 2 );
788
788
errs ++ ;
@@ -804,14 +804,14 @@ apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
804
804
goto out ;
805
805
}
806
806
}
807
- filearr [files ++ ] = mfn ;
807
+ filearr [files ++ ] = estrndup ( mfn , mflen ) ;
808
808
}
809
809
closedir (dir );
810
810
qsort (filearr , files , sizeof (* filearr ), cmpstrp );
811
811
for (i = 0 ; i < files ; i ++ ) {
812
812
load_1 (ms , action , filearr [i ], & errs , & marray ,
813
813
& marraycount );
814
- free (filearr [i ]);
814
+ efree (filearr [i ]);
815
815
}
816
816
free (filearr );
817
817
} else
@@ -886,9 +886,14 @@ apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
886
886
mentrycount += marray [i ].cont_count ;
887
887
}
888
888
out :
889
- for (i = 0 ; i < marraycount ; i ++ )
890
- efree (marray [i ].mp );
891
- efree (marray );
889
+ for (i = 0 ; i < marraycount ; i ++ ) {
890
+ if (marray [i ].mp ) {
891
+ efree (marray [i ].mp );
892
+ }
893
+ }
894
+ if (marray ) {
895
+ efree (marray );
896
+ }
892
897
if (errs ) {
893
898
* magicp = NULL ;
894
899
* nmagicp = 0 ;
@@ -1165,6 +1170,9 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
1165
1170
return -1 ;
1166
1171
}
1167
1172
me = & (* mentryp )[* nmentryp - 1 ];
1173
+ if (me -> mp == NULL ) {
1174
+ return -1 ;
1175
+ }
1168
1176
if (me -> cont_count == me -> max_count ) {
1169
1177
struct magic * nm ;
1170
1178
size_t cnt = me -> max_count + ALLOC_CHUNK ;
@@ -1329,6 +1337,10 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
1329
1337
if (m -> type == FILE_INVALID ) {
1330
1338
if (ms -> flags & MAGIC_CHECK )
1331
1339
file_magwarn (ms , "type `%s' invalid" , l );
1340
+ if (me -> mp ) {
1341
+ efree (me -> mp );
1342
+ me -> mp = NULL ;
1343
+ }
1332
1344
return -1 ;
1333
1345
}
1334
1346
@@ -2219,6 +2231,7 @@ apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
2219
2231
mm = emalloc ((size_t )st .sb .st_size );
2220
2232
if (php_stream_read (stream , mm , (size_t )st .sb .st_size ) != (size_t )st .sb .st_size ) {
2221
2233
file_badread (ms );
2234
+ ret = 1 ;
2222
2235
goto error1 ;
2223
2236
}
2224
2237
ret = 1 ;
0 commit comments