Skip to content

Disallow use of type variables for variables with static storage. #684

Closed
@dtarditi

Description

@dtarditi

We cannot allow static variables to have types with free type variables. The storage for static variables in generic functions persists and could be used with different generic type instantiations. This creates a soundness issue.

Here is an example of how things can go wrong:

#include <stdbool.h>
#include <stdio.h>

_For_any(T) _Ptr<T> get_set(int  isSetter, _Ptr<T> val) {
	static _Ptr<T> capture;
	if (isSetter) {
		capture = val;
		return 0;
	}
	else
		return capture;
}

int main(int argc, _Array_ptr<_Nt_array_ptr<char>> argv : count(argc)) {
	short j = 0;
	short i = 1;
	get_set<short>(true, &i);
	_Ptr<double> p = get_set<double>(false, 0);
	*p = 3.141516; // corrupt memory
	printf(" j = %d", j);
}

When compiled by the Checked C clang compiler on Windows x64,, the store through p overwrites j. Here is the output.

D:\checkedc1\llvm\tools\clang\test\CheckedC>a.exe
 j = 9214

Metadata

Metadata

Assignees

Labels

work itemThis labels issues that are not exactly bugs but are about improvements.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions