From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:62284 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233920AbhGZNDI (ORCPT ); Mon, 26 Jul 2021 09:03:08 -0400 Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 16QDgjoG010801 for ; Mon, 26 Jul 2021 13:43:36 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding; s=corp-2021-07-09; bh=RIsvjAjpko3DFzF7rNZ3CsSBTzhOtCFR7m2Ikc1aRok=; b=MFNgeh+bpyzcj0U2kZNlhYYuzVpu6oLZauVbdrsV93Hh61p9YrXcok6+uEN01dZ718nj IZ6SZGSs8SA3kxLdBivxBK4bdZlacl5ZaD9bfdqPNkSE5dMxprArpaF2Qs5O9p1Cv1sn pXxhNCUe+ldb0MbolXzwkGCKKrJOgwgdZxcYcla6KgenD4b9VG7HrpAKqbTLeyCCTqTB lhwvcqQnTG5nttibQzJmazODAqKEIpQoQDUVEouRyoaENoachbRr1dp7BEjZwWx/K9zw wG6HlHU+RDhntDHlXbC7VnmDj5rjZauAX/ce0K3um2vTH2orWZMAtrwCDYT5sl0n0tP3 cA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding; s=corp-2020-01-29; bh=RIsvjAjpko3DFzF7rNZ3CsSBTzhOtCFR7m2Ikc1aRok=; b=O5HKzwoSQGN/GPEk6eguCvafU0OIN9nLFLb7UbxxVz1qH69q7caNtkWu3xdxVdqe2kiJ 9i6JkdjzCBq4yDmNPMX3PcjISGJJPplawHgbgSI2pI7R6SzV+gWumfY4u/wk8Jna7Gfm eDmQNIcloXiVI5N8LYeQjViC/z2zWfSkTjq300fZYTpIVHj31CelUMz+qi/OEZEapYhM ebaYq3wOnG05enH+7wQiYWyHtz9aDKFek5ALjrPVFamVdIFJYJHOvhC0r9VIGM7uWyFc l87UNBiiCpssWwicsJKw/K9q+hz6Z02j/pstO4zqGKs3lOzjM5XBOhBVGFCjp7iDLXlZ XQ== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3a1cmb1h8b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 26 Jul 2021 13:43:36 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 16QDegQj099212 for ; Mon, 26 Jul 2021 13:43:35 GMT Received: from pps.reinject (localhost [127.0.0.1]) by userp3020.oracle.com with ESMTP id 3a0vmt0ma4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 26 Jul 2021 13:43:35 +0000 Received: from userp3020.oracle.com (userp3020.oracle.com [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 16QDhZFL113361 for ; Mon, 26 Jul 2021 13:43:35 GMT From: Harshvardhan Jha Subject: [PATCH] check_do_while_loop_limit: implements checker for do_while loops Date: Mon, 26 Jul 2021 19:13:19 +0530 Message-Id: <20210726134319.3380-1-harshvardhan.jha@oracle.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-ID: To: smatch@vger.kernel.org Cc: dan.carpenter@oracle.com, Harshvardhan Jha do{} while(n--) loops end at n=-1 but programmers sometimes assume that they end at n=0. This checker sends a warning in these scenarios. Signed-off-by: Harshvardhan Jha --- check_do_while_loop_limit.c | 67 +++++++++++++++++++++++++++++++++++++ check_list.h | 1 + 2 files changed, 68 insertions(+) create mode 100644 check_do_while_loop_limit.c diff --git a/check_do_while_loop_limit.c b/check_do_while_loop_limit.c new file mode 100644 index 00000000..9a492725 --- /dev/null +++ b/check_do_while_loop_limit.c @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2021 Harshvardhan Jha. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt + */ + +#include "smatch.h" +#include "smatch_slist.h" +#include "smatch_extra.h" + +static int my_id; + +STATE(post_minus); + +static bool is_post_minus(struct expression *expr) +{ + expr = strip_expr(expr); + if (!expr) + return false; + if (expr->type != EXPR_POSTOP) + return false; + if (expr->op != SPECIAL_DECREMENT) + return false; + return true; +} + +static void match_post_loop(struct expression *expr) +{ + struct statement *stmt; + + expr = strip_expr(expr); + stmt = expr_get_parent_stmt(expr); + + if (!stmt) + return; + if (stmt->type != STMT_ITERATOR) + return; + if (!is_post_minus(stmt->iterator_post_condition)) + return; + if (is_post_minus(stmt->iterator_post_condition)) + set_state_expr(my_id, stmt->iterator_post_condition->left, &post_minus); +} + +static void match_condition(struct expression *expr) +{ + if (get_state_expr(my_id, expr) != &post_minus) + return; + sm_warning("do while ends on '%s == -1'",expr_to_str(expr)); +} + +void check_do_while_loop_limit(int id) +{ + my_id = id; + add_hook(&match_post_loop, CONDITION_HOOK); + add_hook(&match_condition, CONDITION_HOOK); +} diff --git a/check_list.h b/check_list.h index a57715ea..4d18a173 100644 --- a/check_list.h +++ b/check_list.h @@ -171,6 +171,7 @@ CK(check_syscall_arg_type) CK(check_trinity_generator) CK(register_param_bits_set) CK(register_param_bits_clear) +CK(check_do_while_loop_limit) /* <- your test goes here */ /* CK(register_template) */ -- 2.32.0