@@ -281,6 +281,9 @@ static bool isNullablePointer(const Token* tok)
281
281
282
282
void CheckNullPointer::nullPointerByDeRefAndCheckToken (const Token *tok, const bool printInconclusive)
283
283
{
284
+ if (!tok)
285
+ return ;
286
+
284
287
if (Token::Match (tok, " %num%|%char%|%str%" ))
285
288
return ;
286
289
@@ -311,13 +314,26 @@ const Token * CheckNullPointer::nullPointerByDeRefAndCheck(const Token *start, c
311
314
{
312
315
for (const Token* tok = start; tok != end; tok = tok->next ()) {
313
316
if (Token::simpleMatch (tok, " while (" )) {
317
+ if (!tok->next () || !tok->next ()->link () || !tok->next ()->astOperand2 ()) {
318
+ /* Garbage code */
319
+ return end;
320
+ }
321
+
314
322
/* Analyze tokens inside condition */
315
323
const Token *condTok = tok->next ()->astOperand2 ();
316
324
const Token *startCondTok = tok->next ();
317
325
const Token *endCondTok = tok->next ()->link ();
326
+ if (!Token::simpleMatch (startCondTok, " (" ) || !Token::simpleMatch (endCondTok, " )" )) {
327
+ /* Garbage code */
328
+ return end;
329
+ }
318
330
tok = nullPointerByDeRefAndCheck (startCondTok, endCondTok, printInconclusive);
319
331
320
332
if (condTok->hasKnownIntValue () && condTok->getKnownIntValue () == 0 ) {
333
+ if (!tok->next ()) {
334
+ /* Garbage code */
335
+ return end;
336
+ }
321
337
/* Skip body */
322
338
if (tok->next ()->link ())
323
339
tok = tok->next ()->link ();
@@ -326,33 +342,74 @@ const Token * CheckNullPointer::nullPointerByDeRefAndCheck(const Token *start, c
326
342
bool prevBranchTaken = false ;
327
343
do {
328
344
if (prevBranchTaken) {
345
+ if (!tok->next ()) {
346
+ /* Garbage code */
347
+ return end;
348
+ }
329
349
tok = tok->next ()->linkAt (3 ); /* Skip condition */
350
+ if (!tok || !tok->next () || !tok->next ()->link ()) {
351
+ /* Garbage code */
352
+ return end;
353
+ }
330
354
tok = tok->next ()->link (); /* Skip body */
331
355
continue ;
332
356
}
333
357
358
+ if (!tok->next () || !tok->next ()->next ()) {
359
+ /* Garbage code */
360
+ return end;
361
+ }
362
+
334
363
/* Analyze condition */
335
364
const Token *ifTok = Token::simpleMatch (tok, " if (" ) ? tok : tok->next ()->next ()->next ();
336
- const Token *elseIfCondTok = ifTok->next ()->astOperand2 ();
337
- const Token *elseIfCondStartTok = ifTok->next ();
338
- const Token *elseIfCondEndTok = ifTok->next ()->link ();
339
- tok = nullPointerByDeRefAndCheck (elseIfCondStartTok, elseIfCondEndTok, printInconclusive);
365
+ if (!ifTok || !ifTok->next () || !ifTok->next ()->link () || !ifTok->next ()->astOperand2 ()) {
366
+ /* Garbage code */
367
+ return end;
368
+ }
369
+ const Token *condTok = ifTok->next ()->astOperand2 ();
370
+ const Token *condStartTok = ifTok->next ();
371
+ const Token *condEndTok = ifTok->next ()->link ();
372
+ if (!Token::simpleMatch (condStartTok, " (" ) || !Token::simpleMatch (condEndTok, " )" )) {
373
+ /* Garbage code */
374
+ return end;
375
+ }
376
+ tok = nullPointerByDeRefAndCheck (condStartTok, condEndTok, printInconclusive);
377
+ if (!tok || !tok->next () || !tok->next ()->link ()) {
378
+ /* Garbage code */
379
+ return end;
380
+ }
340
381
341
- prevBranchTaken = prevBranchTaken || (elseIfCondTok->hasKnownIntValue () && elseIfCondTok->getKnownIntValue ());
342
- if (elseIfCondTok->hasKnownIntValue () && elseIfCondTok->getKnownIntValue () == 0 ) {
382
+ prevBranchTaken = prevBranchTaken || (condTok->hasKnownIntValue () && condTok->getKnownIntValue ());
383
+ if (condTok->hasKnownIntValue () && condTok->getKnownIntValue () == 0 ) {
384
+ if (!tok->next ()->link ()) {
385
+ /* Garbage code */
386
+ return end;
387
+ }
343
388
/* Skip body */
344
389
tok = tok->next ()->link ();
345
390
} else {
346
391
/* Analyze body */
347
392
const Token *elseIfBodyStartTok = tok->next ();
348
393
const Token *elseIfBodyEndTok = elseIfBodyStartTok->link ();
394
+ if (!Token::simpleMatch (elseIfBodyStartTok, " {" ) || !Token::simpleMatch (elseIfBodyEndTok, " }" )) {
395
+ /* Garbage code */
396
+ return end;
397
+ }
349
398
tok = nullPointerByDeRefAndCheck (elseIfBodyStartTok, elseIfBodyEndTok, printInconclusive);
350
399
}
400
+ if (!tok || !tok->next ()) {
401
+ /* Garbage code */
402
+ return end;
403
+ }
351
404
} while (Token::simpleMatch (tok->next (), " else { if (" ) && Token::simpleMatch (tok->next ()->linkAt (3 ), " ) {" ));
352
405
353
406
/* Handle final else branch */
354
407
if (prevBranchTaken && Token::simpleMatch (tok->next (), " else" )) {
355
408
/* Skip body */
409
+ if (!tok->next () || !tok->next ()->next () || !tok->next ()->next ()->link ()) {
410
+ /* Garbage code */
411
+ return end;
412
+ }
356
413
tok = tok->next ()->next ()->link ();
357
414
}
358
415
} else if (Token::simpleMatch (tok, " ?" ) && Token::simpleMatch (tok->astOperand2 (), " :" )) { // ternary operator
@@ -364,11 +421,15 @@ const Token * CheckNullPointer::nullPointerByDeRefAndCheck(const Token *start, c
364
421
}
365
422
} else {
366
423
if (isUnevaluated (tok)) {
424
+ if (!tok->next () || !tok->next ()->link ()) {
425
+ /* Garbage code */
426
+ return end;
427
+ }
428
+
367
429
tok = tok->next ()->link ();
368
- continue ;
430
+ } else {
431
+ nullPointerByDeRefAndCheckToken (tok, printInconclusive);
369
432
}
370
-
371
- nullPointerByDeRefAndCheckToken (tok, printInconclusive);
372
433
}
373
434
}
374
435
0 commit comments