zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 8692c6fc0da831421855c27008dc046dcf59fca7 (tree)
parent f04782785f66879db1f31dd6620cd31161c4da08
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Mon,  8 Jul 2019 02:10:26 -0400

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.

Diffstat:
Msrc/target.cpp | 11+++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git 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<ZigGLibCVersion>(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<Slice<uint8_t>> 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; }