diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 457f87460d6e579e02c0e7fadbb71b5210000d52..5382f7287397d3aabd832a4f7f615216ce3d495e 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -408,6 +408,7 @@ sub ctx_statement_block {
 		# context.
 		if ($off >= $len) {
 			for (; $remain > 0; $line++) {
+				last if (!defined $lines[$line]);
 				next if ($lines[$line] =~ /^-/);
 				$remain--;
 				$loff = $len;