@@ -210,6 +210,12 @@ static void quick_decode_init(apriltag_family_t *family, int maxhamming)
210210 }
211211
212212 struct quick_decode * qd = calloc (1 , sizeof (struct quick_decode ));
213+ if (!qd ) {
214+ errno = ENOMEM ;
215+ return ;
216+ }
217+ family -> impl = qd ;
218+
213219 qd -> maxhamming = maxhamming ;
214220 qd -> ncodes = family -> ncodes ;
215221 qd -> nbits = family -> nbits ;
@@ -225,7 +231,15 @@ static void quick_decode_init(apriltag_family_t *family, int maxhamming)
225231
226232 for (int i = 0 ; i < 4 ; i ++ ) {
227233 qd -> chunk_offsets [i ] = calloc (qd -> capacity + 1 , sizeof (uint16_t ));
234+ if (!qd -> chunk_offsets [i ]) {
235+ errno = ENOMEM ;
236+ goto fail ;
237+ }
228238 qd -> chunk_ids [i ] = calloc (qd -> ncodes , sizeof (uint16_t ));
239+ if (!qd -> chunk_ids [i ]) {
240+ errno = ENOMEM ;
241+ goto fail ;
242+ }
229243 }
230244
231245 // Count frequencies
@@ -245,9 +259,15 @@ static void quick_decode_init(apriltag_family_t *family, int maxhamming)
245259 }
246260
247261 // Populate ids
248- uint16_t * cursors [4 ];
262+ uint16_t * cursors [4 ] = { NULL , NULL , NULL , NULL } ;
249263 for (int i = 0 ; i < 4 ; i ++ ) {
250264 cursors [i ] = malloc ((qd -> capacity + 1 ) * sizeof (uint16_t ));
265+ if (cursors [i ] == NULL ) {
266+ errno = ENOMEM ;
267+ for (int j = 0 ; j < 4 ; j ++ )
268+ free (cursors [j ]);
269+ goto fail ;
270+ }
251271 memcpy (cursors [i ], qd -> chunk_offsets [i ], (qd -> capacity + 1 ) * sizeof (uint16_t ));
252272 }
253273
@@ -265,7 +285,10 @@ static void quick_decode_init(apriltag_family_t *family, int maxhamming)
265285 free (cursors [i ]);
266286 }
267287
268- family -> impl = qd ;
288+ return ;
289+
290+ fail :
291+ quick_decode_uninit (family );
269292}
270293
271294// returns a result with hamming set to 255 if no decode was found.
0 commit comments