authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-08 02:10:26-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-08 02:10:26-04:00
log8692c6fc0da831421855c27008dc046dcf59fca7
treed042d93769ffce699cdf00a12b2f37d79e16c05d
parentf04782785f66879db1f31dd6620cd31161c4da08
signaturelock-open Commit is signed but in an unrecognized format.

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.

1 files changed, 7 insertions(+), 4 deletions(-)

src/target.cpp+7-4
......@@ -492,6 +492,9 @@ Error target_parse_glibc_version(ZigGLibCVersion *glibc_ver, const char *text) {
492492}
493493
494494void get_native_target(ZigTarget *target) {
495 // first zero initialize
496 *target = {};
497
495498 ZigLLVM_OSType os_type;
496499 ZigLLVM_ObjectFormatType oformat; // ignored; based on arch/os
497500 ZigLLVMGetNativeTarget(
......@@ -506,7 +509,6 @@ void get_native_target(ZigTarget *target) {
506509 if (target->abi == ZigLLVM_UnknownEnvironment) {
507510 target->abi = target_default_abi(target->arch, target->os);
508511 }
509 target->glibc_version = nullptr;
510512 if (target_is_glibc(target)) {
511513 target->glibc_version = allocate<ZigGLibCVersion>(1);
512514 *target->glibc_version = {2, 17, 0};
......@@ -703,6 +705,10 @@ Error target_parse_abi(ZigLLVM_EnvironmentType *out_abi, const char *abi_ptr, si
703705
704706Error target_parse_triple(ZigTarget *target, const char *triple) {
705707 Error err;
708
709 // first initialize all to zero
710 *target = {};
711
706712 SplitIterator it = memSplit(str(triple), str("-"));
707713
708714 Optional<Slice<uint8_t>> opt_archsub = SplitIterator_next(&it);
......@@ -732,9 +738,6 @@ Error target_parse_triple(ZigTarget *target, const char *triple) {
732738 } else {
733739 target->abi = target_default_abi(target->arch, target->os);
734740 }
735
736 target->vendor = ZigLLVM_UnknownVendor;
737 target->is_native = false;
738741 return ErrorNone;
739742}
740743