Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Zero-init or void-init most char arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
n8sh authored and thewilsonator committed Jan 4, 2019
1 parent 29e9ac2 commit c322ec4
Show file tree
Hide file tree
Showing 78 changed files with 576 additions and 573 deletions.
3 changes: 3 additions & 0 deletions changelog/zero_init.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`char`/`wchar` fields in most D runtime (`core.*` and `rt.*`) structs are now zero-initialized

Fields that are single `char`/wchar` or fixed-length arrays of such are now initialized to all zero bits instead of all one bits for most structs in the D runtime (`core.*` and `rt.*` modules). This simplifies initialization and removes the need to store init data when it makes the entire struct zero-initialized. Most affected structs are used for interoperability with C APIs.
4 changes: 2 additions & 2 deletions src/core/cpuid.d
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ struct CpuFeatures
bool probablyIntel; // true = _probably_ an Intel processor, might be faking
bool probablyAMD; // true = _probably_ an AMD processor
string processorName;
char [12] vendorID;
char [48] processorNameBuffer;
char [12] vendorID = 0;
char [48] processorNameBuffer = 0;
uint features = 0; // mmx, sse, sse2, hyperthreading, etc
uint miscfeatures = 0; // sse3, etc.
uint extfeatures = 0; // HLE, AVX2, RTM, etc.
Expand Down
4 changes: 2 additions & 2 deletions src/core/internal/utf.d
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ void encode(ref char[] s, dchar c)
}
else
{
char[4] buf;
char[4] buf = void;
uint L;

if (c <= 0x7FF)
Expand Down Expand Up @@ -516,7 +516,7 @@ void encode(ref wchar[] s, dchar c)
}
else
{
wchar[2] buf;
wchar[2] buf = void;

buf[0] = cast(wchar) ((((c - 0x10000) >> 10) & 0x3FF) + 0xD800);
buf[1] = cast(wchar) (((c - 0x10000) & 0x3FF) + 0xDC00);
Expand Down
2 changes: 1 addition & 1 deletion src/core/runtime.d
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ Throwable.TraceInfo defaultTraceHandler( void* ptr = null )
int ret = 0;
for ( int i = FIRSTFRAME; i < numframes; ++i )
{
char[4096] fixbuf;
char[4096] fixbuf = void;
auto buf = framelist[i][0 .. strlen(framelist[i])];
auto pos = cast(size_t)(i - FIRSTFRAME);
buf = fixline( buf, fixbuf );
Expand Down
22 changes: 11 additions & 11 deletions src/core/stdc/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ else version (FreeBSD)

union __mbstate_t // <sys/_types.h>
{
char[128] _mbstate8;
char[128] _mbstate8 = 0;
long _mbstateL;
}
}
Expand Down Expand Up @@ -231,7 +231,7 @@ else version (NetBSD)

union __mbstate_t // <sys/_types.h>
{
char[128] _mbstate8;
char[128] _mbstate8 = 0;
long _mbstateL;
}
}
Expand Down Expand Up @@ -261,7 +261,7 @@ else version (OpenBSD)

union __mbstate_t // <sys/_types.h>
{
char[128] __mbstate8;
char[128] __mbstate8 = 0;
long __mbstateL;
}
}
Expand Down Expand Up @@ -300,7 +300,7 @@ else version (DragonFlyBSD)

union __mbstate_t // <sys/stdint.h>
{
char[128] _mbstate8;
char[128] _mbstate8 = 0;
long _mbstateL;
}
}
Expand Down Expand Up @@ -453,7 +453,7 @@ else version (CRuntime_Glibc)
int _old_offset;
ushort _cur_column;
byte _vtable_offset;
char[1] _shortbuf;
char[1] _shortbuf = 0;
void* _lock;
}

Expand All @@ -466,7 +466,7 @@ else version (CRuntime_Musl)
{
union fpos_t
{
char[16] __opaque;
char[16] __opaque = 0;
double __align;
}
struct _IO_FILE;
Expand Down Expand Up @@ -593,7 +593,7 @@ else version (NetBSD)

int function(void *) _flush;
/* Formerly used by fgetln/fgetwln; kept for binary compatibility */
char[__sbuf.sizeof - _flush.sizeof] _lb_unused;
char[__sbuf.sizeof - _flush.sizeof] _lb_unused = void;


int _blksize;
Expand Down Expand Up @@ -696,8 +696,8 @@ else version (Solaris)
char* _ptr;
int _cnt;
char* _base;
char _flag;
char _magic;
char _flag = 0;
char _magic = 0;
ushort __flags; // __orientation:2
// __ionolock:1
// __seekable:1
Expand Down Expand Up @@ -780,7 +780,7 @@ else version (CRuntime_UClibc)
struct __STDIO_FILE_STRUCT
{
ushort __modeflags;
char[2] __ungot_width;
char[2] __ungot_width = 0;
int __filedes;
char* __bufstart;
char* __bufend;
Expand All @@ -791,7 +791,7 @@ else version (CRuntime_UClibc)
__STDIO_FILE_STRUCT* __nextopen;
void *__cookie;
_IO_cookie_io_functions_t __gcs;
wchar_t[2] __ungot;
wchar_t[2] __ungot = 0;
mbstate_t __state;
void *__unused;
int __user_locking;
Expand Down
8 changes: 4 additions & 4 deletions src/core/stdc/wchar_.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ version (CRuntime_Glibc)
int __count;
union ___value
{
wint_t __wch;
wint_t __wch = 0;
char[4] __wchb;
}
___value __value;
Expand All @@ -44,7 +44,7 @@ else version (OpenBSD)
{
union __mbstate_t
{
char[128] __mbstate8;
char[128] __mbstate8 = 0;
int64_t __mbstateL;
}
alias mbstate_t = __mbstate_t;
Expand All @@ -69,8 +69,8 @@ else version (CRuntime_UClibc)
///
struct mbstate_t
{
wchar_t __mask;
wchar_t __wc;
wchar_t __mask = 0;
wchar_t __wc = 0;
}
}
else
Expand Down
24 changes: 12 additions & 12 deletions src/core/sys/darwin/mach/loader.d
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ version (CoreDdoc)
uint cmdsize;

/// The name of this segment.
char[16] segname;
char[16] segname = 0;

/// Memory address of this segment.
uint vmaddr;
Expand Down Expand Up @@ -181,7 +181,7 @@ version (CoreDdoc)
uint cmdsize;

/// The name of this segment.
char[16] segname;
char[16] segname = 0;

/// Memory address of this segment.
long vmaddr;
Expand Down Expand Up @@ -212,10 +212,10 @@ version (CoreDdoc)
struct section
{
/// The name of this this section.
char[16] sectname;
char[16] sectname = 0;

/// The name of the segment this section belongs to.
char[16] segname;
char[16] segname = 0;

/// The memory address of this section.
uint addr;
Expand Down Expand Up @@ -249,10 +249,10 @@ version (CoreDdoc)
struct section_64
{
/// The name of this this section.
char[16] sectname;
char[16] sectname = 0;

/// The name of the segment this section belongs to.
char[16] segname;
char[16] segname = 0;

/// The memory address of this section.
ulong addr;
Expand Down Expand Up @@ -351,7 +351,7 @@ struct segment_command
{
uint cmd;
uint cmdsize;
char[16] segname;
char[16] segname = 0;
uint vmaddr;
uint vmsize;
uint fileoff;
Expand All @@ -366,7 +366,7 @@ struct segment_command_64
{
uint cmd;
uint cmdsize;
char[16] segname;
char[16] segname = 0;
long vmaddr;
long vmsize;
long fileoff;
Expand All @@ -379,8 +379,8 @@ struct segment_command_64

struct section
{
char[16] sectname;
char[16] segname;
char[16] sectname = 0;
char[16] segname = 0;
uint addr;
uint size;
uint offset;
Expand All @@ -394,8 +394,8 @@ struct section

struct section_64
{
char[16] sectname;
char[16] segname;
char[16] sectname = 0;
char[16] segname = 0;
ulong addr;
ulong size;
uint offset;
Expand Down
2 changes: 1 addition & 1 deletion src/core/sys/darwin/netinet/in_.d
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ static if (_DARWIN_C_SOURCE)
struct ip_opts
{
in_addr ip_dst;
char[40] ip_opts;
char[40] ip_opts = 0;
};

enum IP_OPTIONS = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/core/sys/dragonflybsd/sys/elf32.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ alias Elf32_Sword Elf32_Ssize;

struct Elf32_Ehdr
{
char[EI_NIDENT] e_ident;
char[EI_NIDENT] e_ident = 0;
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Expand Down
2 changes: 1 addition & 1 deletion src/core/sys/dragonflybsd/sys/elf64.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ alias Elf64_Sxword Elf64_Ssize;

struct Elf64_Ehdr
{
char[EI_NIDENT] e_ident;
char[EI_NIDENT] e_ident = 0;
Elf64_Half e_type;
Elf64_Half e_machine;
Elf64_Word e_version;
Expand Down
2 changes: 1 addition & 1 deletion src/core/sys/freebsd/sys/elf32.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ alias Elf32_Sword Elf32_Ssize;

struct Elf32_Ehdr
{
char[EI_NIDENT] e_ident;
char[EI_NIDENT] e_ident = 0;
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Expand Down
2 changes: 1 addition & 1 deletion src/core/sys/freebsd/sys/elf64.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ alias Elf64_Sxword Elf64_Ssize;

struct Elf64_Ehdr
{
char[EI_NIDENT] e_ident;
char[EI_NIDENT] e_ident = 0;
Elf64_Half e_type;
Elf64_Half e_machine;
Elf64_Word e_version;
Expand Down
18 changes: 9 additions & 9 deletions src/core/sys/freebsd/sys/mount.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct fid
{
ushort fid_len;
ushort fid_data0;
char[MAXFIDSZ] fid_data;
char[MAXFIDSZ] fid_data = 0;
}

enum MFSNAMELEN = 16;
Expand All @@ -55,10 +55,10 @@ struct statfs_t
uint f_namemax;
uid_t f_owner;
fsid_t f_fsid;
char[80] f_charspare;
char[MFSNAMELEN] f_fstypename;
char[MNAMELEN] f_mntfromname;
char[MNAMELEN] f_mntonname;
char[80] f_charspare = 0;
char[MFSNAMELEN] f_fstypename = 0;
char[MNAMELEN] f_mntfromname = 0;
char[MNAMELEN] f_mntonname = 0;
}


Expand Down Expand Up @@ -207,7 +207,7 @@ struct nfs_public
struct vfsconf
{
uint vfc_version;
char[MFSNAMELEN] vfc_name;
char[MFSNAMELEN] vfc_name = 0;
vfsops* vfc_vfsops;
int vfc_typenum;
int vfc_refcount;
Expand All @@ -219,7 +219,7 @@ struct vfsconf
struct xvfsconf
{
vfsops* vfc_vfsops;
char[MFSNAMELEN] vfc_name;
char[MFSNAMELEN] vfc_name = 0;
int vfc_typenum;
int vfc_refcount;
int vfc_flags;
Expand All @@ -230,7 +230,7 @@ struct xvfsconf
struct ovfsconf
{
void* vfc_vfsops;
char[32] vfc_name;
char[32] vfc_name = 0;
int vfc_index;
int vfc_refcount;
int vfc_flags;
Expand All @@ -252,7 +252,7 @@ struct vfsidctl
{
int vc_vers;
fsid_t vc_fsid;
char[MFSNAMELEN] vc_fstypename;
char[MFSNAMELEN] vc_fstypename = 0;
fsctlop_t vc_op;
void* vc_ptr;
size_t vc_len;
Expand Down
4 changes: 2 additions & 2 deletions src/core/sys/linux/elf.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ enum EI_NIDENT = 16;

struct Elf32_Ehdr
{
char[EI_NIDENT] e_ident;
char[EI_NIDENT] e_ident = 0;
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Expand All @@ -60,7 +60,7 @@ struct Elf32_Ehdr

struct Elf64_Ehdr
{
char[EI_NIDENT] e_ident;
char[EI_NIDENT] e_ident = 0;
Elf64_Half e_type;
Elf64_Half e_machine;
Elf64_Word e_version;
Expand Down
2 changes: 1 addition & 1 deletion src/core/sys/linux/netinet/in_.d
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ version (linux_libc)
struct ip_opts
{
in_addr ip_dst;
char[40] ip_opts;
char[40] ip_opts = 0;
};

struct ip_mreqn
Expand Down
2 changes: 1 addition & 1 deletion src/core/sys/netbsd/sys/elf32.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ alias Elf32_Sword Elf32_Ssize;

struct Elf32_Ehdr
{
char[EI_NIDENT] e_ident;
char[EI_NIDENT] e_ident = 0;
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Expand Down
2 changes: 1 addition & 1 deletion src/core/sys/netbsd/sys/elf64.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ alias Elf64_Sxword Elf64_Ssize;

struct Elf64_Ehdr
{
char[EI_NIDENT] e_ident;
char[EI_NIDENT] e_ident = 0;
Elf64_Half e_type;
Elf64_Half e_machine;
Elf64_Word e_version;
Expand Down

0 comments on commit c322ec4

Please sign in to comment.