Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the uid_t type signed or unsigned?

Tags:

c

uid

I know that the standard doesn't say anything about the signedness of uid_t or gid_t.

Inconsistency:

Page http://www.gnu.org/software/libc/manual/html_node/Reading-Persona.html says:

In the GNU C Library, this is an alias for unsigned int.

But man setreuid says:

Supplying a value of -1 for either the real or effective user ID forces the system to leave that ID unchanged.

Questions:

  1. So, is uid_t signed or unsigned in the GNU Library?

  2. How can I supply -1 if uid_t and gid_t are unsigned (-1 will be converted to 0xFFFFFFFF)?

like image 325
Antonio Rizzo Avatar asked Jan 26 '14 22:01

Antonio Rizzo


2 Answers

uid_t is (after some typedefs/defines) defined as __U32_TYPE which is defined as unsigned int (that is on my Gentoo Linux system).

However, just because -1 has a special meaning it does not mean that UIDs are restricted to the numbers that fit in a signed int. It just means that the highest value (i.e. (unsigned int)-1) is not a valid UID. The code in setreuid probably uses the reverse form of that cast ((signed int)ruid) to compare against -1 cleanly although it accepts an uid_t.

like image 190
ThiefMaster Avatar answered Oct 20 '22 00:10

ThiefMaster


POSIX says explicitly that pid_t is a signed integer type used for representing process and process group IDs.

It says that ino_t is an unsigned integer type used for file serial numbers.

For both uid_t and gid_t, it says that the type is an integer type (no mention of signed or unsigned) used for user IDs and group IDS.

Thus, the standard clearly states that the type of uid_t and gid_t can be signed or unsigned depending on the platform. It is very reasonable to suppose that this indeterminacy arose because actual implementations used both signed and unsigned types, and the standard was designed not to invalidate existing implementations.

like image 21
Jonathan Leffler Avatar answered Oct 19 '22 22:10

Jonathan Leffler