Skip to content

Commit

Permalink
xdr: store chars consistently
Browse files Browse the repository at this point in the history
Cast char's through unsigned char before storing as an integer in
xdr_char(), this ensures that the encoded form is consistently not
sign-extended following Open Solaris's example.

Prior to this change, platforms with signed chars would sign extend
values with the high bit set but ones with unsigned chars would not
so 0xff would be stored as 0x000000ff on unsigned char platforms and
0xffffffff on signed char platforms.  Decoding has the same
result for either form so this is a largely cosmetic change, but it
seems best to produce consistent output.

For more discussion, see openzfs/zfs#14173

Reviewed by:	mav, imp
Differential Revision:	https://reviews.freebsd.org/D37992
  • Loading branch information
brooksdavis committed Jan 12, 2023
1 parent fe33e0a commit a872c37
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/libc/xdr/xdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,13 +429,13 @@ xdr_uint16_t(XDR *xdrs, uint16_t *u_int16_p)
bool_t
xdr_char(XDR *xdrs, char *cp)
{
int i;
u_int i;

i = (*cp);
if (!xdr_int(xdrs, &i)) {
i = *((unsigned char *)cp);
if (!xdr_u_int(xdrs, &i)) {
return (FALSE);
}
*cp = i;
*((unsigned char *)cp) = i;
return (TRUE);
}

Expand Down
8 changes: 4 additions & 4 deletions sys/xdr/xdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,13 @@ xdr_uint16_t(XDR *xdrs, uint16_t *uint16_p)
bool_t
xdr_char(XDR *xdrs, char *cp)
{
int i;
u_int i;

i = (*cp);
if (!xdr_int(xdrs, &i)) {
i = *((unsigned char *)cp);
if (!xdr_u_int(xdrs, &i)) {
return (FALSE);
}
*cp = i;
*((unsigned char *)cp) = i;
return (TRUE);
}

Expand Down

0 comments on commit a872c37

Please sign in to comment.