Skip to content

Constant checked function pointer #481

@nmeum

Description

@nmeum

I am using the following Checked C compiler version:

$ clang --version
clang version 6.0.0 (https://github.com/Microsoft/checkedc-clang dfbae3d3076014ac4c5571fd29a917a0e6f62912) (https://github.com/Microsoft/checkedc-llvm d8d78c7a13472b344e429d1ae320bc4f01ecb6db)                                              
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/checkedc-llvm/bin

And I am trying to compile the following code:

struct myops {
	_Ptr<void (void)> myfptr;
};

struct mystruct {
	const struct myops *ops
	    : itype(_Ptr<const struct myops>);
};

void
strfunc(void)
{
	return;
}

void
myfunc(struct mystruct *s)
{
	s->ops->myfptr();
}

int
main(void)
{
	struct myops o;
	struct mystruct m;

	o.myfptr = strfunc;
	m.ops = &o;

	myfunc(&m);
	return 0;
}

Which yields the following error message:

test.c:19:10: error: cast to checked function pointer type '_Ptr<void (void)>' from incompatible checked pointer type '_Ptr<void (void)> const'                                                                                               
        s->ops->myfptr();
        ~~~~~~~~^~~~~~
1 error generated.

However, if I change the definition of myops to:

struct myops {
	void (*myfptr)(void);
};

The code compiles fine, it also compiles fine if I remove the const keyword from the ops member of the struct mystruct.

Having read section 3.8 of the Checked C language specification it is unclear to me why the compiler defines the function pointer as const and why it tries to convert it to a non-const function pointer up on invocation. Is this a bug in the compiler?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions