Linux-Sparse Archive on lore.kernel.org
 help / color / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Ben Dooks <ben.dooks@codethink.co.uk>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 06/13] format-check: add a function to check to type of strings
Date: Wed, 14 Oct 2020 01:22:24 +0200
Message-ID: <20201013232231.10349-7-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20201013232231.10349-1-luc.vanoostenryck@gmail.com>

This checking is currently done by check_assignment_types()
but this is not adequate, for example because it allows
a void pointer for the string argument while a warning
should be issued.

So, add a custom checking function.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 verify-format.c | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/verify-format.c b/verify-format.c
index 0ef2cb863ae9..90fe0ede0431 100644
--- a/verify-format.c
+++ b/verify-format.c
@@ -106,12 +106,44 @@ static int printf_fmt_numtype(struct format_type *fmt,
 	return check_assignment_types(fmt->type, expr, typediff);
 }
 
+// For 's' & 'S' specifiers
+static const char *check_printf_string(struct format_type *fmt, struct symbol *source)
+{
+	const char *typediff = "different base types";
+	struct symbol *target = fmt->type;
+
+	examine_pointer_target(target);
+	examine_pointer_target(source);
+
+	if (type_class(source, &source) != CLASS_PTR)
+		return typediff;
+	if (source->ctype.as)
+		return "different address spaces";
+
+	// get the base type
+	type_class(source->ctype.base_type, &source);
+	type_class(source, &source);
+
+	if (target == &const_string_ctype) {
+		if (source == &char_ctype)
+			return NULL;
+		if (source == &uchar_ctype)
+			return NULL;
+		if (source == &schar_ctype)
+			return NULL;
+	} if (target == &const_wstring_ctype) {
+		if (source == wchar_ctype)
+			return NULL;
+	}
+	return typediff;
+}
+
 static int printf_fmt_string(struct format_type *fmt,
 			     struct expression **expr,
 			     struct symbol *ctype,
 			     const char **typediff)
 {
-	return check_assignment_types(fmt->type, expr, typediff);
+	return !(*typediff = check_printf_string(fmt, ctype));
 }
 
 static int printf_fmt_pointer(struct format_type *fmt,
-- 
2.28.0


  parent reply index

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-13 23:22 [PATCH 00/13] format-check: add specific type checking Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 01/13] format-check: void * is not OK for strings, fix the test Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 02/13] format-check: more complete parsing of the length & type modifiers Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 03/13] format-check: add helper type_class() Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 04/13] format-check: merge 'fmt_string' & 'string' Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 05/13] format-check: remove unneeded member: target Luc Van Oostenryck
2020-10-13 23:22 ` Luc Van Oostenryck [this message]
2020-10-13 23:22 ` [PATCH 07/13] format-check: add a function to check to type of 'n' arguments Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 08/13] format-check: add a function to check to type of pointers Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 09/13] format-check: remove printf_fmt_print_pointer() Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 10/13] format-check: add a function to check the type of floats Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 11/13] format-check: add a function to check the type of integers Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 12/13] format-check: remove wrappers around type checking methods Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 13/13] format-check: simplify calling of parse_printf_get_fmt() Luc Van Oostenryck

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=20201013232231.10349-7-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=ben.dooks@codethink.co.uk \
    --cc=linux-sparse@vger.kernel.org \
    /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

Linux-Sparse Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-sparse/0 linux-sparse/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-sparse linux-sparse/ https://lore.kernel.org/linux-sparse \
		linux-sparse@vger.kernel.org
	public-inbox-index linux-sparse

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-sparse


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git