Re: [PATCH 2/6] parse: initial parsing of __attribute__((format))

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



iOn 04/04/2019 12:48, Luc Van Oostenryck wrote:
On Thu, Apr 04, 2019 at 10:53:08AM +0100, Ben Dooks wrote:
On 03/04/2019 21:46, Luc Van Oostenryck wrote:
On Wed, Apr 03, 2019 at 04:35:48PM +0100, Ben Dooks wrote:
@@ -2981,8 +3071,18 @@ struct token *external_declaration(struct token *token, struct symbol_list **lis
   		if (!(decl->ctype.modifiers & MOD_STATIC))
   			decl->ctype.modifiers |= MOD_EXTERN;
+
+		base_type->ctype.printf_msg = decl->ctype.printf_msg;
+		base_type->ctype.printf_va_start = decl->ctype.printf_va_start;
   	} else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
   		sparse_error(token->pos, "void declaration");
+	} else if (base_type && base_type->type == SYM_PTR) {
+		// think this is correct to do //
+		struct symbol *ptr_base = get_base_type(base_type);
+		if (ptr_base) {
+			ptr_base->ctype.printf_msg = decl->ctype.printf_msg;
+			ptr_base->ctype.printf_va_start = decl->ctype.printf_va_start;
+		}

I'll need to check this.
What situation is this supposed to cover?

definition of pointers to functions, ie:

	int (*ptr)(args..) __attribute__((format...));

But this is not the correct syntax as it applies the attribute
to the declared object 'ptr', a pointer type and not to the
underlying function type.

The syntax used in the kernel is (cfr. kdb_printf_t):
	__attribute__((...)) int (*ptr)(args...);
and GCC's manual also specifies:
	int (__attribute__((...)) *ptr)(args...);
(but I'm not sure this one is correctly supported by sparse).

It would be good to issue a warning if the 'format' attribute is
applied to anything that is not a function type.

ok, gcc doesn't seem to issue a warning about that.
in fact, it seems happy about:

int main(void)
{
  int (*ptr)(const char *msg, ...) __attribute__((format(printf,1,2)));

  (ptr)("moose %s %d\n", 1, 2);
  return 0;
}

gcc -O2 -Wall test4.c

test4.c: In function ‘main’:
test4.c:6:17: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
   (ptr)("moose %s %d\n", 1, 2);
                ~^        ~
                %d

--
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux