News for hackers. Stuff that matters.
Re:Comparative unsafety by thaumasiotes on Friday April 09, 2021 @10:33AM (#26751068)
> Why am I using my own ffi version of chown instead of the libc crate? The libc crate prototypes chown with unsigned uid_t and gid_t types, and I want to pass -1 because I'm not interested in changing the group. I'll spare you the long rant about how one should never redeclare system interfaces if you can't take the time to do so properly, because it turns out if you dig into it, uid_t boils down to uint32_t, but even so, the manual for chown says -1 should work and I want it to work.

First thing I'd try would be to pass "~0" instead of "-1". Would that work? (Looks like in Rust that would be "!0". I don't know anything about Rust.)

Re:Comparative unsafety by boardwaalk on Friday April 09, 2021 @02:19PM (#26754026)
If your reaction to Rust actually following the prototype of chmod but not accepting conversions signed/unsigned silently is to redefine chmod so you can do it like you’d do in C… that seems a little bullheaded to me. Wrapping a C function is dangerous in a literal and real sense, doing “-1 as u32” is not at all.
Re:Comparative unsafety by thaumasiotes on Saturday April 10, 2021 @01:27AM (#26759156)
> If your reaction to Rust actually following the prototype of chmod but not accepting conversions

Eh... as the article points out, the chown documentation specifically says that you are expected to provide negative numbers. The spec is obviously bugged.

This limits the amount of righteousness you can claim over "but we followed the spec (that we knew was wrong)!"

Re:Comparative unsafety by NovemberWhiskey on Friday April 09, 2021 @07:19PM (#26757373)
If you read the POSIX specification for chown you'll see that the "leave unchanged" sentinel value is, for UIDs, defined to be (uid_t) -1.

I can't be bothered to try it myself but I'm fairly sure the author could just have written (-1i32) as uid_t and gotten some of their life back.

Re:Comparative unsafety by cryptonector on Friday April 09, 2021 @11:45PM (#26758743)
Yes. Specifically, POSIX does not specify whether id_t, uid_t, gid_t, or pid_t are signed or unsigned, so whenever it refers to the "default"/"leave alone"/"unspecified" value, it always does it as a cast of -1.
Re:Comparative unsafety by adwn on Friday April 09, 2021 @10:54AM (#26751300)
Yes, !0 would work. Or, to make the intent even more clear:

    -1_i32 as u32
Definitely better than declaring your own function prototype.
Re:Comparative unsafety by ChrisSD on Friday April 09, 2021 @10:58AM (#26751358)
In Rust the simplest way would be to use the `u32::MAX` constant. Create a fun alias for it if you want to express intent.
Re:Comparative unsafety by thaumasiotes on Friday April 09, 2021 @11:09AM (#26751476)
I actually find ~0 more intuitive. u32::MAX makes me think the relevant concept is "a really big number"; ~0 makes me think the relevant concept is "a bunch of bits that are all 1s".
Re:Comparative unsafety by ChrisSD on Friday April 09, 2021 @11:20AM (#26751631)
Fair enough. But tbh I'm not sure it really matters in this context. It's just a constant that means "ignore this parameter". Whether it's "a really big number" or "a bunch of bits that are all 1s" is incidental.

Btw, you can directly express "all ones" in Rust as:

    0b_1111_1111_1111_1111_1111_1111_1111_1111
Though it's somewhat less succinct. ;)

EDIT: Added more ones, thanks adwn!

Re:Comparative unsafety by adwn on Friday April 09, 2021 @11:26AM (#26751718)
> 0b1111_1111_1111_1111

That's just 16 bits and will result in the value 65535, not -1.

Re:Comparative unsafety by ChrisSD on Friday April 09, 2021 @11:29AM (#26751772)
Ha, yes you're right. I'm on my phone and got tired of writing ones. I'll edit when I'm next at my laptop.
Re:Comparative unsafety by cryptonector on Friday April 09, 2021 @11:46PM (#26758752)
Although `~0` assumes twos-complement signed numbers. Which, well, is probably a safe assumption, but still.
Re:Comparative unsafety by IshKebab on Saturday April 10, 2021 @09:28AM (#26761340)
All modern computers use twos-complement and modern languages like Rust require it. Not assuming twos-complement is a theoretical nitpick along the lines of not assuming a byte is 8 bits. Nobody does that.