Skip to content

Commit

Permalink
needed to pass vector pointer-to-pointer to ensure no use after frees
Browse files Browse the repository at this point in the history
  • Loading branch information
prathameshd30 committed Jul 12, 2024
1 parent beb1bce commit 00e2cc3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ Initially, I was going to write it in the OOP C Pattern (pointers to functions i

### To Do
- [x] Look into GitHub actions based testing
- [x] Valgrind testing?
- [x] How can I integrate CMake, CTest, GitHub actions and Valgrind?
- [ ] Write primitive as well as pointer-to-structs examples
- [ ] Examples should try to break the framework as much as possible
- [x] Valgrind testing?
- [ ] Use constant pointers, pointers to constants and constant pointers to constant wherever applicable.


### Checklist
- Each function, with pointer params, should check for NULL for each pointer,
Expand Down
15 changes: 8 additions & 7 deletions include/data_structures/vector/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,20 @@ bool linear_search_##NAME(NAME* obj, TYPE ref ,bool (*comparator)(TYPE, TYPE), u
}\
return false;\
}\
bool delete_##NAME(NAME* obj, bool (*delete_type)(TYPE)){\
if(!obj){\
bool delete_##NAME(NAME** obj, bool (*delete_type)(TYPE)){\
if(!obj || !*obj){\
return false;\
}\
if(obj->data){\
if((*obj)->data){\
if(delete_type){\
for(uint64_t i = 0; i<obj->size; ++i){\
delete_type(obj->data[i]);\
for(uint64_t i = 0; i<(*obj)->size; ++i){\
delete_type((*obj)->data[i]);\
}\
}\
free(obj->data);\
free((*obj)->data);\
}\
free(obj);\
free(*(obj));\
*obj = NULL;\
return true;\
}\
bool clear_##NAME(NAME* obj, bool (*delete_type)(TYPE), TYPE val){\
Expand Down
6 changes: 3 additions & 3 deletions tests/data_structures/vector/vector_primitive_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main(void){
puts("Empty vector not nulled");
return -1;
}
delete_intVec(iv,NULL);
delete_intVec(&iv,NULL);
if(iv!=NULL){
puts("Deleted integer vector still points to memory");
return -1;
Expand All @@ -60,7 +60,7 @@ int main(void){
puts("Vector data is NULL even though size is not 0");
return -1;
}
delete_intVec(iv, NULL);
delete_intVec(&iv, NULL);
if(iv!=NULL){
puts("Deleted vector of +ve size is not NULL");
return -1;
Expand All @@ -76,7 +76,7 @@ int main(void){
puts("Vector data is NULL at size UINT64_MAX");
return -1;
}
delete_intVec(iv,NULL);
delete_intVec(&iv,NULL);
if(iv!=NULL){
puts("Vector is not null after being freed at UINT64_MAX");
return -1;
Expand Down

0 comments on commit 00e2cc3

Please sign in to comment.