New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes for zero-sized allocation #1045
Conversation
@@ -1848,10 +1849,10 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) { | |||
} | |||
|
|||
assert(!struct_type->data.structure.zero_bits_loop_flag); | |||
assert(struct_type->data.structure.fields); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed assertion.
assert(decl_node->type == NodeTypeContainerDecl); | ||
|
||
size_t field_count = struct_type->data.structure.src_field_count; | ||
assert(struct_type->data.structure.fields != nullptr || field_count == 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Attempted tweak.
@@ -1964,7 +1965,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) { | |||
struct_type->di_type = replacement_di_type; | |||
return; | |||
} | |||
assert(struct_type->di_type); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed assertion.
@@ -1964,7 +1965,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) { | |||
struct_type->di_type = replacement_di_type; | |||
return; | |||
} | |||
assert(struct_type->di_type); | |||
assert(struct_type->di_type != nullptr || field_count == 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Attempted tweak.
@@ -1976,7 +1977,8 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) { | |||
// if you hit this assert then probably this type or a related type didn't | |||
// get ensure_complete_type called on it before using it with something that | |||
// requires a complete type | |||
assert(LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref) > 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed assertion.
@@ -1976,7 +1977,8 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) { | |||
// if you hit this assert then probably this type or a related type didn't | |||
// get ensure_complete_type called on it before using it with something that | |||
// requires a complete type | |||
assert(LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref) > 0); | |||
assert(LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref) > 0 || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Attempted tweak.
@@ -5922,20 +5922,20 @@ static const uint8_t int_sizes_in_bits[] = { | |||
}; | |||
|
|||
struct CIntTypeInfo { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Padding fix discussed in #993.
@@ -73,6 +76,9 @@ ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate_nonzero(size_t count) { | |||
|
|||
template<typename T> | |||
ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate(size_t count) { | |||
if (count == 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes behavior for zero sized allocation portable.
T *ptr = reinterpret_cast<T*>(realloc(old, new_count * sizeof(T))); | ||
if (!ptr) | ||
zig_panic("allocation failed"); | ||
static inline T *reallocate(T *old, size_t old_count, size_t new_count) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implement reallocate
using reallocate_nonzero
to avoid code duplication.
@@ -7647,7 +7647,7 @@ static TypeTableEntry *get_error_set_intersection(IrAnalyze *ira, TypeTableEntry | |||
assert(errors[error_entry->value] == nullptr); | |||
errors[error_entry->value] = error_entry; | |||
} | |||
ZigList<ErrorTableEntry *> intersection_list = {}; | |||
ZigList<ErrorTableEntry *> intersection_list = {0}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will see this in a few places. ZigList
needs to be initialized in order to guarantee the members are zeroed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you cite a source showing that {0}
is necessary and {}
is not sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually you are right: https://stackoverflow.com/a/1069634/1930331. Maybe I'm thinking about C, but C++ will work.
See #1044.