Received: from eggs.gnu.org ([2001:4830:134:3::10]:37015) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aJNaV-0004Ev-AX for qemu-devel@nongnu.org; Wed, 13 Jan 2016 10:43:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aJNaS-0001Cw-HU for qemu-devel@nongnu.org; Wed, 13 Jan 2016 10:43:55 -0500 Received: from mx1.redhat.com ([209.132.183.28]:60089) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aJNaS-0001CX-AU for qemu-devel@nongnu.org; Wed, 13 Jan 2016 10:43:52 -0500 From: Markus Armbruster Date: Wed, 13 Jan 2016 16:43:39 +0100 Message-Id: <1452699819-26608-42-git-send-email-armbru@redhat.com> In-Reply-To: <1452699819-26608-1-git-send-email-armbru@redhat.com> References: <1452699819-26608-1-git-send-email-armbru@redhat.com> Subject: [Qemu-devel] [PULL 41/41] checkpatch: Detect newlines in error_report and other error functions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: "Jason J. Herne" From: "Jason J. Herne" We don't want newlines embedded in error messages. This seems to be a common problem with new code so let's try to catch it with checkpatch. This will not catch cases where newlines are inserted into the middle of an existing multi-line statement. But those cases should be rare. Signed-off-by: Jason J. Herne Message-Id: <1449858642-24267-1-git-send-email-jjherne@linux.vnet.ibm.com> [Rephrased "Error function text" to "Error messages", dropped error_vprintf, error_printf, error_printf from $qemu_error_funcs, because they may legitimately print newlines] Signed-off-by: Markus Armbruster --- scripts/checkpatch.pl | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index efca817..257126f 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -2498,6 +2498,42 @@ sub process { WARN("use QEMU instead of Qemu or QEmu\n" . $herecurr); } +# Qemu error function tests + + # Find newlines in error messages + my $qemu_error_funcs = qr{error_setg| + error_setg_errno| + error_setg_win32| + error_set| + error_vreport| + error_report}x; + + if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(\s*\".*\\n/) { + WARN("Error messages should not contain newlines\n" . $herecurr); + } + + # Continue checking for error messages that contains newlines. This + # check handles cases where string literals are spread over multiple lines. + # Example: + # error_report("Error msg line #1" + # "Error msg line #2\n"); + my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"}; + my $continued_str_literal = qr{\+\s*\".*\"}; + + if ($rawline =~ /$quoted_newline_regex/) { + # Backtrack to first line that does not contain only a quoted literal + # and assume that it is the start of the statement. + my $i = $linenr - 2; + + while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) { + $i--; + } + + if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) { + WARN("Error messages should not contain newlines\n" . $herecurr); + } + } + # check for non-portable ffs() calls that have portable alternatives in QEMU if ($line =~ /\bffs\(/) { ERROR("use ctz32() instead of ffs()\n" . $herecurr); -- 2.4.3