From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:17610 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233719AbhGZNK1 (ORCPT ); Mon, 26 Jul 2021 09:10:27 -0400 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 16QDl3cX031182 for ; Mon, 26 Jul 2021 13:50:56 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : content-type : in-reply-to : mime-version; s=corp-2021-07-09; bh=MtGx1FJC+DTaUDvpPU4Zwe1QuTZI7ku+91O1Ngda2+w=; b=L0T8dpFtNgulnmx771xMdnA51QQlwX5VNrc2r2BqRk/2t9wigdzqXUF5DDMu/TZJoeE7 F79z5iRiF0N7NmZSR0y9PnOoGKtmMS1zq6Jf6o5RNjz6WNLLaCMvwcwYO3pHcBZfSIcT b8vdpgBLaTWo9KdvqqIy2+SxRhzvmABIVNAKS9YIWwnK3Q+7t5o2thgc+H77oWpI3UyK zJJw4KCibQzxGNAkJtczZypui6wF9Z5momZvVYT1VFKG/wNjpYukgk5efh+uMThWeBT8 TlIB0Ml8wwhGm1n4RozXDq1ZnbPdHqwGx2wLmYWK4psrVb3qAQ2ETabyJT5iSh9pj8DQ 8A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : content-type : in-reply-to : mime-version; s=corp-2020-01-29; bh=MtGx1FJC+DTaUDvpPU4Zwe1QuTZI7ku+91O1Ngda2+w=; b=VyOuMYAPtS8RJQJoU8INL+itBdmXvBNxw+phH3HuRZibGtTitBCMomfjvgNDHB0zR9ZA wiT7Me2F4+etwycnVp5hkD7UjZuEgxwKSdO7bmRVMR8etm2TWTKQMah3MroVXIevCtEs y9H+Oqc0qvsT7Gd8sUZxrTbpwBf90fhabw0JZnUe+kxSxQIqIo6Q6EWasnJMBnQZVWXK mztaLnekyrTTxjg3B/Nzd/BtmjpSlBHCI/dkjUwfWSKqc2542xjTWv5Rxup5tE0XvkTi /vifaLfu7RJfCGVhJtS/tGF5LjkJYMR0cDLqObT1Yop5U7AX7d+VyXb8ZGrEG+T69jTK sw== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3a1up6rd5r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 26 Jul 2021 13:50:56 +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 16QDooPX146469 for ; Mon, 26 Jul 2021 13:50:55 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2106.outbound.protection.outlook.com [104.47.55.106]) by userp3020.oracle.com with ESMTP id 3a0vmt0y5n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 26 Jul 2021 13:50:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=MtGx1FJC+DTaUDvpPU4Zwe1QuTZI7ku+91O1Ngda2+w=; b=MfEAnyYkZ/orYOe1/hZqGaVkpX04TECtfsAFz1rt21DAwFs1ssZCdxRD4JKA7KknKAeasx/t4JBtsty8GncxBs230F74OE3XIUYApslk+yH+t59YqyET1H04LMHIRKiXpLo4YXw1CxI6s8TVnSupjlbjTQW3oBHUxW+Ojim22pY= Date: Mon, 26 Jul 2021 16:50:31 +0300 From: Dan Carpenter Subject: Re: [PATCH] check_do_while_loop_limit: implements checker for do_while loops Message-ID: <20210726135031.GK25548@kadam> References: <20210726134319.3380-1-harshvardhan.jha@oracle.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210726134319.3380-1-harshvardhan.jha@oracle.com> MIME-Version: 1.0 List-ID: To: Harshvardhan Jha Cc: smatch@vger.kernel.org On Mon, Jul 26, 2021 at 07:13:19PM +0530, Harshvardhan Jha wrote: > 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 should be copyright Oracle (Harshvardhan has been working for Oracle as an intern this summer). > + * > + * 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)); ^ Add a space after the comma. > +} regards, dan carpenter