Some of these aren't ever actually misaligned on targets we currently
support, since stack-allocated meemory is at a minimum aligned as 4. But
it's still good to fix them to not rely on this behavior.
Signed-off-by: Sebastian <sebastian@sebsite.pw>
---
net/dial/resolve.ha | 5 ++---
net/ip/+freebsd.ha | 5 ++++-
net/ip/+linux.ha | 5 ++++-
net/ip/+openbsd.ha | 5 ++++-
temp/+freebsd.ha | 5 ++---
temp/+linux.ha | 5 ++---
6 files changed, 18 insertions(+), 12 deletions(-)
diff --git a/net/dial/resolve.ha b/net/dial/resolve.ha
index d2774ef7..1231fdc6 100644
--- a/net/dial/resolve.ha
+++ b/net/dial/resolve.ha
@@ -106,9 +106,8 @@ fn resolve_addr(addr: str) ([]ip::addr | error) = {
const domain = dns::parse_domain(addr);
defer free(domain);
- let rand: []u8 = [0, 0];
- random::buffer(rand);
- let id = *(&rand[0]: *u16);
+ let id = 0u16;
+ random::buffer(&id: *[size(u16)]u8);
const query6 = dns::message {
header = dns::header {
diff --git a/net/ip/+freebsd.ha b/net/ip/+freebsd.ha
index e66feb03..8c439abb 100644
--- a/net/ip/+freebsd.ha
+++ b/net/ip/+freebsd.ha
@@ -7,12 +7,15 @@ use rt;
export fn to_native(a: addr, port: u16) rt::sockaddr = {
match (a) {
case let v4: addr4 =>
+ const addr = rt::in_addr {
+ s_addr = endian::host.getu32(v4),
+ };
return rt::sockaddr {
in = rt::sockaddr_in {
sin_len = size(rt::in_addr): u8,
sin_family = rt::AF_INET,
sin_port = endian::htonu16(port),
- sin_addr = rt::in_addr { s_addr = *(&v4[0]: *opaque: *u32) },
+ sin_addr = addr,
...
},
...
diff --git a/net/ip/+linux.ha b/net/ip/+linux.ha
index 2157fdf6..9db23591 100644
--- a/net/ip/+linux.ha
+++ b/net/ip/+linux.ha
@@ -7,11 +7,14 @@ use rt;
export fn to_native(a: addr, port: u16) rt::sockaddr = {
match (a) {
case let v4: addr4 =>
+ const addr = rt::in_addr {
+ s_addr = endian::host.getu32(v4),
+ };
return rt::sockaddr {
in = rt::sockaddr_in {
sin_family = rt::AF_INET,
sin_port = endian::htonu16(port),
- sin_addr = rt::in_addr { s_addr = *(&v4[0]: *opaque: *u32) },
+ sin_addr = addr,
...
},
...
diff --git a/net/ip/+openbsd.ha b/net/ip/+openbsd.ha
index 45d6f37c..8c439abb 100644
--- a/net/ip/+openbsd.ha
+++ b/net/ip/+openbsd.ha
@@ -7,12 +7,15 @@ use rt;
export fn to_native(a: addr, port: u16) rt::sockaddr = {
match (a) {
case let v4: addr4 =>
+ const addr = rt::in_addr {
+ s_addr = endian::host.getu32(v4),
+ };
return rt::sockaddr {
in = rt::sockaddr_in {
sin_len = size(rt::in_addr): u8,
sin_family = rt::AF_INET,
sin_port = endian::htonu16(port),
- sin_addr = rt::in_addr { s_addr = *(&v4[0]: *u32) },
+ sin_addr = addr,
...
},
...
diff --git a/temp/+freebsd.ha b/temp/+freebsd.ha
index 5e9ec4a7..5be353ca 100644
--- a/temp/+freebsd.ha
+++ b/temp/+freebsd.ha
@@ -50,10 +50,9 @@ export fn named(
static let pathbuf = path::buffer { ... };
static let namebuf: [32]u8 = [0...];
for (true) {
- let rand: [size(u64)]u8 = [0...];
- random::buffer(rand);
+ let id = 0u64;
+ random::buffer(&id: *[size(u64)]u8);
- const id = *(&rand[0]: *u64);
const name = fmt::bsprintf(namebuf, "temp.{}", id);
const path = path::set(&pathbuf, path, name)!;
diff --git a/temp/+linux.ha b/temp/+linux.ha
index da71718d..b7466b65 100644
--- a/temp/+linux.ha
+++ b/temp/+linux.ha
@@ -60,10 +60,9 @@ export fn named(
static let pathbuf = path::buffer { ... };
static let namebuf: [32]u8 = [0...];
for (true) {
- let rand: [size(u64)]u8 = [0...];
- random::buffer(rand);
+ let id = 0u64;
+ random::buffer(&id: *[size(u64)]u8);
- const id = *(&rand[0]: *u64);
const name = fmt::bsprintf(namebuf, "temp.{}", id);
const path = path::set(&pathbuf, path, name)!;
--
2.45.2
On Sat, Sep 07, 2024 at 04:40:27PM -0400, Sebastian wrote:
> Some of these aren't ever actually misaligned on targets we currently
> support, since stack-allocated meemory is at a minimum aligned as 4. But
> it's still good to fix them to not rely on this behavior.
sure, ok.
> Signed-off-by: Sebastian <sebastian@sebsite.pw>
> ---
> net/dial/resolve.ha | 5 ++---
> net/ip/+freebsd.ha | 5 ++++-
> net/ip/+linux.ha | 5 ++++-
> net/ip/+openbsd.ha | 5 ++++-
> temp/+freebsd.ha | 5 ++---
> temp/+linux.ha | 5 ++---
> 6 files changed, 18 insertions(+), 12 deletions(-)
>
> diff --git a/net/dial/resolve.ha b/net/dial/resolve.ha
> index d2774ef7..1231fdc6 100644
> --- a/net/dial/resolve.ha
> +++ b/net/dial/resolve.ha
> @@ -106,9 +106,8 @@ fn resolve_addr(addr: str) ([]ip::addr | error) = {
> const domain = dns::parse_domain(addr);
> defer free(domain);
>
> - let rand: []u8 = [0, 0];
> - random::buffer(rand);
> - let id = *(&rand[0]: *u16);
> + let id = 0u16;
> + random::buffer(&id: *[size(u16)]u8);
>
> const query6 = dns::message {
> header = dns::header {
> diff --git a/net/ip/+freebsd.ha b/net/ip/+freebsd.ha
> index e66feb03..8c439abb 100644
> --- a/net/ip/+freebsd.ha
> +++ b/net/ip/+freebsd.ha
> @@ -7,12 +7,15 @@ use rt;
> export fn to_native(a: addr, port: u16) rt::sockaddr = {
> match (a) {
> case let v4: addr4 =>
> + const addr = rt::in_addr {
> + s_addr = endian::host.getu32(v4),
> + };
> return rt::sockaddr {
> in = rt::sockaddr_in {
> sin_len = size(rt::in_addr): u8,
> sin_family = rt::AF_INET,
> sin_port = endian::htonu16(port),
> - sin_addr = rt::in_addr { s_addr = *(&v4[0]: *opaque: *u32) },
> + sin_addr = addr,
> ...
> },
> ...
> diff --git a/net/ip/+linux.ha b/net/ip/+linux.ha
> index 2157fdf6..9db23591 100644
> --- a/net/ip/+linux.ha
> +++ b/net/ip/+linux.ha
> @@ -7,11 +7,14 @@ use rt;
> export fn to_native(a: addr, port: u16) rt::sockaddr = {
> match (a) {
> case let v4: addr4 =>
> + const addr = rt::in_addr {
> + s_addr = endian::host.getu32(v4),
> + };
> return rt::sockaddr {
> in = rt::sockaddr_in {
> sin_family = rt::AF_INET,
> sin_port = endian::htonu16(port),
> - sin_addr = rt::in_addr { s_addr = *(&v4[0]: *opaque: *u32) },
> + sin_addr = addr,
> ...
> },
> ...
> diff --git a/net/ip/+openbsd.ha b/net/ip/+openbsd.ha
> index 45d6f37c..8c439abb 100644
> --- a/net/ip/+openbsd.ha
> +++ b/net/ip/+openbsd.ha
> @@ -7,12 +7,15 @@ use rt;
> export fn to_native(a: addr, port: u16) rt::sockaddr = {
> match (a) {
> case let v4: addr4 =>
> + const addr = rt::in_addr {
> + s_addr = endian::host.getu32(v4),
> + };
> return rt::sockaddr {
> in = rt::sockaddr_in {
> sin_len = size(rt::in_addr): u8,
> sin_family = rt::AF_INET,
> sin_port = endian::htonu16(port),
> - sin_addr = rt::in_addr { s_addr = *(&v4[0]: *u32) },
> + sin_addr = addr,
> ...
> },
> ...
> diff --git a/temp/+freebsd.ha b/temp/+freebsd.ha
> index 5e9ec4a7..5be353ca 100644
> --- a/temp/+freebsd.ha
> +++ b/temp/+freebsd.ha
> @@ -50,10 +50,9 @@ export fn named(
> static let pathbuf = path::buffer { ... };
> static let namebuf: [32]u8 = [0...];
> for (true) {
> - let rand: [size(u64)]u8 = [0...];
> - random::buffer(rand);
> + let id = 0u64;
> + random::buffer(&id: *[size(u64)]u8);
>
> - const id = *(&rand[0]: *u64);
> const name = fmt::bsprintf(namebuf, "temp.{}", id);
> const path = path::set(&pathbuf, path, name)!;
>
> diff --git a/temp/+linux.ha b/temp/+linux.ha
> index da71718d..b7466b65 100644
> --- a/temp/+linux.ha
> +++ b/temp/+linux.ha
> @@ -60,10 +60,9 @@ export fn named(
> static let pathbuf = path::buffer { ... };
> static let namebuf: [32]u8 = [0...];
> for (true) {
> - let rand: [size(u64)]u8 = [0...];
> - random::buffer(rand);
> + let id = 0u64;
> + random::buffer(&id: *[size(u64)]u8);
>
> - const id = *(&rand[0]: *u64);
> const name = fmt::bsprintf(namebuf, "temp.{}", id);
> const path = path::set(&pathbuf, path, name)!;
>
> --
> 2.45.2
>