All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Martin Kaiser <martin@kaiser.cx>
Cc: Larry Finger <Larry.Finger@lwfinger.net>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Guenter Roeck <linux@roeck-us.net>,
	linux-staging@lists.linux.dev, kernel-janitors@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/6] staging: rtl8188eu: use safe iterator in rtw_free_network_queue
Date: Mon, 17 May 2021 18:57:33 +0300	[thread overview]
Message-ID: <20210517155733.GK1955@kadam> (raw)
In-Reply-To: <20210516160613.30489-1-martin@kaiser.cx>

Thanks for catching these...  I've created a new Smatch static checker
warning for this but it only works for list_for_each_entry().
Eventually someone would have run the coccinelle script to convert these
list_for_each loops into list_for_each_entry().  Otherwise you have to
parse container_of() and I've been meaning to do that for a while but I
haven't yet.

Anyway, I'm going to test it out overnight and see what it finds.  It's
sort a new use for the modification_hook(), before I had only ever used
it to silence warnings but this check uses it to trigger warnings.  So
perhaps it will generate a lot of false positives.  We'll see.

It sets the state of the iterator to &start at the start of the loop
and if it's not &start state at the end then it prints a warning.

regards,
dan carpenter

/*
 * Copyright (C) 2021 Oracle.
 *
 * 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_extra.h"

static int my_id;

STATE(start);
STATE(watch);

static struct statement *iterator_stmt, *pre_stmt, *post_stmt;

static void set_watch(struct sm_state *sm, struct expression *mod_expr)
{
	set_state(my_id, sm->name, sm->sym, &watch);
}

static bool is_list_macro(const char *macro)
{
	if (strcmp(macro, "list_for_each_entry") == 0)
		return true;
	return false;
}

static void match_iterator_statement(struct statement *stmt)
{
	const char *macro;

	if (stmt->type != STMT_ITERATOR ||
	    !stmt->iterator_pre_statement ||
	    !stmt->iterator_post_statement)
		return;

	macro = get_macro_name(stmt->pos);
	if (!macro)
		return;
	if (!is_list_macro(macro))
		return;

	iterator_stmt = stmt;
	pre_stmt = stmt->iterator_pre_statement;
	post_stmt = stmt->iterator_post_statement;
}

static bool stmt_matches(struct expression *expr, struct statement *stmt)
{
	struct expression *tmp;
	struct statement *parent;

	if (!stmt)
		return false;
	while ((tmp = expr_get_parent_expr(expr)))
		expr = tmp;

	parent = expr_get_parent_stmt(expr);
	return parent == stmt;
}

static char *get_iterator_member(void)
{
	struct expression *expr;

	if (!iterator_stmt ||
	    !iterator_stmt->iterator_pre_condition)
		return NULL;

	expr = iterator_stmt->iterator_pre_condition;
	if (expr->type != EXPR_PREOP || expr->op != '!')
		return NULL;
	expr = strip_parens(expr->unop);
	if (expr->type != EXPR_COMPARE)
		return NULL;
	expr = strip_parens(expr->left);
	if (expr->type != EXPR_PREOP || expr->op != '&')
		return NULL;
	expr = strip_expr(expr->unop);
	if (expr->type != EXPR_DEREF || !expr->member)
		return NULL;
	return expr->member->name;
}

static void match_pre_statement(struct expression *expr)
{
	char *name, *member;
	struct symbol *sym;
	char buf[64];

	if (!stmt_matches(expr, pre_stmt))
		return;

	name = expr_to_var_sym(expr->left, &sym);
	if (!name)
		return;
	member = get_iterator_member();

	snprintf(buf, sizeof(buf), "%s->%s.next", name, member);
	set_state(my_id, buf, sym, &start);
}

static void match_post_statement(struct expression *expr)
{
	struct smatch_state *state;
	char *name, *member;
	struct symbol *sym;
	char buf[64];

	if (!stmt_matches(expr, post_stmt))
		return;

	name = expr_to_var_sym(expr->left, &sym);
	if (!name)
		return;
	member = get_iterator_member();

	snprintf(buf, sizeof(buf), "%s->%s.next", name, member);
	state = get_state(my_id, buf, sym);
	if (!state || state == &start)
		return;

	sm_warning("iterator '%s' changed during iteration", buf);
}

void check_list_set_inside(int id)
{
	my_id = id;

	if (option_project != PROJ_KERNEL)
		return;

	add_hook(match_iterator_statement, STMT_HOOK);
	add_hook(match_pre_statement, ASSIGNMENT_HOOK);
	add_hook(match_post_statement, ASSIGNMENT_HOOK);

	add_modification_hook(my_id, &set_watch);
}


  parent reply	other threads:[~2021-05-17 18:09 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-16 16:06 [PATCH 1/6] staging: rtl8188eu: use safe iterator in rtw_free_network_queue Martin Kaiser
2021-05-16 16:06 ` [PATCH 2/6] staging: rtl8188eu: use safe iterator in rtw_free_all_stainfo Martin Kaiser
2021-05-16 19:24   ` Guenter Roeck
2021-05-16 16:06 ` [PATCH 3/6] staging: rtl8188eu: use safe iterator in expire_timeout_chk Martin Kaiser
2021-05-16 19:24   ` Guenter Roeck
2021-05-16 16:06 ` [PATCH 4/6] staging: rtl8188eu: use safe iterator in rtw_acl_remove_sta Martin Kaiser
2021-05-16 19:24   ` Guenter Roeck
2021-05-16 16:06 ` [PATCH 5/6] staging: rtl8188eu: use safe iterator in rtw_sta_flush Martin Kaiser
2021-05-16 19:24   ` Guenter Roeck
2021-05-16 16:06 ` [PATCH 6/6] staging: rtl8188eu: use safe iterator in rtw_free_xmitframe_queue Martin Kaiser
2021-05-16 19:24   ` Guenter Roeck
2021-05-16 19:24 ` [PATCH 1/6] staging: rtl8188eu: use safe iterator in rtw_free_network_queue Guenter Roeck
2021-05-16 20:03 ` Christophe JAILLET
2021-05-17 20:21   ` Martin Kaiser
2021-05-17 15:57 ` Dan Carpenter [this message]
2021-05-18  8:28   ` Dan Carpenter
2021-05-19 14:16     ` Dan Carpenter
2021-05-19 14:16     ` [PATCH] staging: emxx_udc: fix loop in _nbu2ss_nuke() Dan Carpenter
2021-05-19 14:17     ` [PATCH] w1: fix loop in w1_fini() Dan Carpenter
2023-05-08  8:59       ` (subset) " Krzysztof Kozlowski
2021-05-19 14:18     ` [bug report] {net, IB}/mlx5: Manage port association for multiport RoCE Dan Carpenter
2021-05-20  8:09       ` Leon Romanovsky
2021-05-19 14:19     ` [bug report] Bluetooth: L2CAP: Add initial code for Enhanced Credit Based Mode Dan Carpenter
2021-05-19 14:20     ` [PATCH] scsi: libsas: use _safe() loop in sas_resume_port() Dan Carpenter
2021-05-19 14:48       ` John Garry
2021-05-22  4:40       ` Martin K. Petersen
2021-05-17 20:18 ` [PATCH v2 1/6] staging: rtl8188eu: use safe iterator in rtw_free_network_queue Martin Kaiser
2021-05-17 20:18   ` [PATCH v2 2/6] staging: rtl8188eu: use safe iterator in rtw_free_all_stainfo Martin Kaiser
2021-05-17 20:32     ` Guenter Roeck
2021-05-17 20:18   ` [PATCH v2 3/6] staging: rtl8188eu: use safe iterator in expire_timeout_chk Martin Kaiser
2021-05-17 20:33     ` Guenter Roeck
2021-05-17 20:18   ` [PATCH v2 4/6] staging: rtl8188eu: use safe iterator in rtw_acl_remove_sta Martin Kaiser
2021-05-17 20:34     ` Guenter Roeck
2021-05-17 20:18   ` [PATCH v2 5/6] staging: rtl8188eu: use safe iterator in rtw_sta_flush Martin Kaiser
2021-05-17 20:34     ` Guenter Roeck
2021-05-17 20:18   ` [PATCH v2 6/6] staging: rtl8188eu: use safe iterator in rtw_free_xmitframe_queue Martin Kaiser
2021-05-17 20:35     ` Guenter Roeck
2021-05-17 20:30   ` [PATCH v2 1/6] staging: rtl8188eu: use safe iterator in rtw_free_network_queue Guenter Roeck
2021-05-18  7:44   ` Dan Carpenter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210517155733.GK1955@kadam \
    --to=dan.carpenter@oracle.com \
    --cc=Larry.Finger@lwfinger.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=linux@roeck-us.net \
    --cc=martin@kaiser.cx \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.