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) {...@@ -492,6 +492,9 @@ Error target_parse_glibc_version(ZigGLibCVersion *glibc_ver, const char *text) {
492}492}
493493
494void get_native_target(ZigTarget *target) {494void get_native_target(ZigTarget *target) {
495 // first zero initialize
496 *target = {};
497
495 ZigLLVM_OSType os_type;498 ZigLLVM_OSType os_type;
496 ZigLLVM_ObjectFormatType oformat; // ignored; based on arch/os499 ZigLLVM_ObjectFormatType oformat; // ignored; based on arch/os
497 ZigLLVMGetNativeTarget(500 ZigLLVMGetNativeTarget(
...@@ -506,7 +509,6 @@ void get_native_target(ZigTarget *target) {...@@ -506,7 +509,6 @@ void get_native_target(ZigTarget *target) {
506 if (target->abi == ZigLLVM_UnknownEnvironment) {509 if (target->abi == ZigLLVM_UnknownEnvironment) {
507 target->abi = target_default_abi(target->arch, target->os);510 target->abi = target_default_abi(target->arch, target->os);
508 }511 }
509 target->glibc_version = nullptr;
510 if (target_is_glibc(target)) {512 if (target_is_glibc(target)) {
511 target->glibc_version = allocate<ZigGLibCVersion>(1);513 target->glibc_version = allocate<ZigGLibCVersion>(1);
512 *target->glibc_version = {2, 17, 0};514 *target->glibc_version = {2, 17, 0};
...@@ -703,6 +705,10 @@ Error target_parse_abi(ZigLLVM_EnvironmentType *out_abi, const char *abi_ptr, si...@@ -703,6 +705,10 @@ Error target_parse_abi(ZigLLVM_EnvironmentType *out_abi, const char *abi_ptr, si
703705
704Error target_parse_triple(ZigTarget *target, const char *triple) {706Error target_parse_triple(ZigTarget *target, const char *triple) {
705 Error err;707 Error err;
708
709 // first initialize all to zero
710 *target = {};
711
706 SplitIterator it = memSplit(str(triple), str("-"));712 SplitIterator it = memSplit(str(triple), str("-"));
707713
708 Optional<Slice<uint8_t>> opt_archsub = SplitIterator_next(&it);714 Optional<Slice<uint8_t>> opt_archsub = SplitIterator_next(&it);
...@@ -732,9 +738,6 @@ Error target_parse_triple(ZigTarget *target, const char *triple) {...@@ -732,9 +738,6 @@ Error target_parse_triple(ZigTarget *target, const char *triple) {
732 } else {738 } else {
733 target->abi = target_default_abi(target->arch, target->os);739 target->abi = target_default_abi(target->arch, target->os);
734 }740 }
735
736 target->vendor = ZigLLVM_UnknownVendor;
737 target->is_native = false;
738 return ErrorNone;741 return ErrorNone;
739}742}
740743