Re: [cc65] cc65 function pointer bugs

From: Ullrich von Bassewitz <uz1musoftware.de>
Date: 2015-03-11 22:35:47
Hi Greg, thanks for the report!

On Wed, Mar 11, 2015 at 01:21:47PM -0400, Greg King wrote:
> cc65 version 2.13.9, Subversion revision 5990, fails to handle some
> operations on function pointers.

Most of the stuff in your program isn't allowed in C.

Here are the relevant parts of the standard (ISO/IEC 9899:1999):

------------------------------------------------------------------------------
6.5.6.2:

For addition, either both operands shall have arithmetic type, or one operand
shall be a pointer to an object type and the other shall have integer type.
(Incrementing is equivalent to adding 1).

6.5.6.3:

For subtraction, one of the following shall hold:
— both operands have arithmetic type;
— both operands are pointers to qualified or unqualified versions of
  compatible object types; or
— the left operand is a pointer to an object type and the right operand has
  integer type.
(Decrementing is equivalent to subtracting 1).

6.5.8.2 (relational operators):

One of the following shall hold:
— both operands have real type;
— both operands are pointers to qualified or unqualified versions of
  compatible object types; or
— both operands are pointers to qualified or unqualified versions of
  compatible incomplete types.


And finally 6.2.5.1:

The meaning of a value stored in an object or returned by a function is
determined by the type of the expression used to access it. (An identifier
declared to be an object is the simplest such expression; the type is
specified in the declaration of the identifier.) Types are partitioned into
object types (types that fully describe objects), function types (types that
describe functions), and incomplete types (types that describe objects but
lack information needed to determine their sizes).
------------------------------------------------------------------------------

So functions aren't object types, which means that you cannot do pointer
arithmetic or use relational operators with function pointers.

It is somewhat confusing that gcc allows function pointer arithmetic as an
extension, and not even "-Wall -Wextra -W" gives any warnings about it. You
will need "-Wpointer-arith" to get warnings, which in case of your program
yields:

test.c:9:5: warning: wrong type argument to increment [-Wpointer-arith]
test.c:12:14: warning: pointer to a function used in subtraction [-Wpointer-arith]
test.c:13:14: warning: pointer to a function used in subtraction [-Wpointer-arith]
test.c:14:15: warning: pointer to a function used in subtraction [-Wpointer-arith]
test.c:15:15: warning: pointer to a function used in subtraction [-Wpointer-arith]
test.c:16:11: warning: pointer to a function used in subtraction [-Wpointer-arith]
test.c:17:11: warning: pointer to a function used in subtraction [-Wpointer-arith]
test.c:18:15: warning: pointer to a function used in subtraction [-Wpointer-arith]
test.c:19:14: warning: pointer to a function used in subtraction [-Wpointer-arith]

Lines 10 and 11 are also invalid C code, but I wasn't able to convince gcc to
output a diagnostic.

Of course, cc65 shouldn't run into internal errors but complain instead.

To summarize:
From your program, lines 7+8 are valid C, but cc65 doesn't accept line 8.
Lines 9-19 are invalid C code. cc65 should definitely not run into internal
errors and preferrably output diagnostics.

I will have a look at that.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz@musoftware.de
Encrypted email preferred                          PGP Key-Id: 29D93B10
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Wed, 11 Mar 2015 22:35:47 +0100

This archive was generated by hypermail 2.1.8 : 2015-03-11 22:35:55 CET