From 8692c6fc0da831421855c27008dc046dcf59fca7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 8 Jul 2019 02:10:26 -0400 Subject: [PATCH] zero initialize target Fixes glibc_version being set to garbage. I've made this mistake before so this is an attempt to prevent future bugs. Zig doesn't have zero-initialization, so are we being a hypocrite by using this C feature? No, because C doesn't have the feature that forces you to initialize all fields. That would have prevented this bug every single time. --- src/target.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/target.cpp b/src/target.cpp index 8240023305..5cbf8c4de1 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -492,6 +492,9 @@ Error target_parse_glibc_version(ZigGLibCVersion *glibc_ver, const char *text) { } void get_native_target(ZigTarget *target) { + // first zero initialize + *target = {}; + ZigLLVM_OSType os_type; ZigLLVM_ObjectFormatType oformat; // ignored; based on arch/os ZigLLVMGetNativeTarget( @@ -506,7 +509,6 @@ void get_native_target(ZigTarget *target) { if (target->abi == ZigLLVM_UnknownEnvironment) { target->abi = target_default_abi(target->arch, target->os); } - target->glibc_version = nullptr; if (target_is_glibc(target)) { target->glibc_version = allocate(1); *target->glibc_version = {2, 17, 0}; @@ -703,6 +705,10 @@ Error target_parse_abi(ZigLLVM_EnvironmentType *out_abi, const char *abi_ptr, si Error target_parse_triple(ZigTarget *target, const char *triple) { Error err; + + // first initialize all to zero + *target = {}; + SplitIterator it = memSplit(str(triple), str("-")); Optional> opt_archsub = SplitIterator_next(&it); @@ -732,9 +738,6 @@ Error target_parse_triple(ZigTarget *target, const char *triple) { } else { target->abi = target_default_abi(target->arch, target->os); } - - target->vendor = ZigLLVM_UnknownVendor; - target->is_native = false; return ErrorNone; }