Skip to content

Commit 9272f58

Browse files
author
Prabhuk
authored
Polymorphic bounds safe interface stdlib functions. (checkedc#306)
Changed the signature of malloc, calloc and free to make them polymorphic bounds safe interface functions and fixed the tests that failed due to the change in signature.
1 parent ee4fe9e commit 9272f58

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

include/stdlib_checked.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ unsigned long long int strtoull(const char * restrict nptr :
6262

6363
// TODO: express alignment constraints once where clauses have been added.
6464
void *aligned_alloc(size_t alignment, size_t size) : byte_count(size);
65-
void *calloc(size_t nmemb, size_t size) : byte_count(nmemb * size);
66-
void free(void *pointer : byte_count(0));
67-
void *malloc(size_t size) : byte_count(size);
65+
_Itype_for_any(T) void *calloc(size_t nmemb, size_t size) : itype(_Array_ptr<T>) byte_count(nmemb * size);
66+
_Itype_for_any(T) void free(void *pointer : itype(_Array_ptr<T>) byte_count(0));
67+
_Itype_for_any(T) void *malloc(size_t size) : itype(_Array_ptr<T>) byte_count(size);
6868
void *realloc(void *pointer : byte_count(1), size_t size) : byte_count(size);
6969

7070
char *getenv(const char *n : itype(_Nt_array_ptr<const char>)) : itype(_Nt_array_ptr<char>);

tests/typechecking/malloc_free.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,50 +37,50 @@ void f4(void) unchecked {
3737

3838
// Test you can always `free` a `malloc`d ptr
3939
void f11(void) {
40-
ptr<int> x = malloc(sizeof(int));
41-
free(x);
40+
ptr<int> x = malloc<int>(sizeof(int));
41+
free<int>(x);
4242
}
4343

4444
// Test you can always `free` a `calloc`d ptr
4545
void f12(void) {
46-
ptr<int> x = calloc(1, sizeof(int));
47-
free(x);
46+
ptr<int> x = calloc<int>(1, sizeof(int));
47+
free<int>(x);
4848
}
4949

5050
// Test you can always `free` a `realloc`d ptr
5151
void f13(void) {
52-
ptr<int> x = malloc(sizeof(int));
52+
ptr<int> x = malloc<int>(sizeof(int));
5353
ptr<int> y = realloc(x, 2 * sizeof(int));
54-
free(y);
54+
free<int>(y);
5555
}
5656

5757
// Test you can always `free` a `aligned_alloc`d ptr
5858
void f14(void) {
5959
ptr<int> x = aligned_alloc(_Alignof(int), sizeof(int));
60-
free(x);
60+
free<int>(x);
6161
}
6262

6363
// Test you can always `free` a `malloc`d array_ptr
6464
void f21(void) {
65-
array_ptr<int> x : count(4) = malloc(4 * sizeof(int));
66-
free(x);
65+
array_ptr<int> x : count(4) = malloc<int>(4 * sizeof(int));
66+
free<int>(x);
6767
}
6868

6969
// Test you can always `free` a `calloc`d array_ptr
7070
void f22(void) {
71-
array_ptr<int> x : count(4) = calloc(4, sizeof(int));
72-
free(x);
71+
array_ptr<int> x : count(4) = calloc<int>(4, sizeof(int));
72+
free<int>(x);
7373
}
7474

7575
// Test you can always `free` a `realloc`d array_ptr
7676
void f23(void) {
77-
array_ptr<int> x : count(4) = malloc(4 * sizeof(int));
77+
array_ptr<int> x : count(4) = malloc<int>(4 * sizeof(int));
7878
array_ptr<int> y : count(8) = realloc(x, 8 * sizeof(int));
79-
free(y);
79+
free<int>(y);
8080
}
8181

8282
// Test you can always `free` a `aligned_alloc`d array_ptr
8383
void f24(void) {
8484
array_ptr<int> x : count(4) = aligned_alloc(_Alignof(int), 4 * sizeof(int));
85-
free(x);
85+
free<int>(x);
8686
}

0 commit comments

Comments
 (0)