Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3ded6fd

Browse files
author
Thomas G. Lockhart
committed
Fix code to do the right thing with mixed-endian clients and servers.
1 parent bf3bcb6 commit 3ded6fd

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/backend/libpq/pqcomprim.c

+15-11
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111

1212
/* --------------------------------------------------------------------- */
13-
/* Is the other way around than system ntoh/hton, so we roll our own
14-
here */
15-
16-
#ifndef BYTE_ORDER
13+
/* These definitions for ntoh/hton are the other way around from the
14+
* default system definitions, so we roll our own here.
15+
*/
16+
17+
#ifndef BYTE_ORDER
1718
#error BYTE_ORDER must be defined as LITTLE_ENDIAN, BIG_ENDIAN or PDP_ENDIAN
1819
#endif
1920

@@ -24,11 +25,13 @@
2425
# define hton_l(n) n
2526
#else /* BYTE_ORDER != LITTLE_ENDIAN */
2627
# if BYTE_ORDER == BIG_ENDIAN
27-
# define ntoh_s(n) (u_short)(((u_char *) &n)[0] << 8 | ((u_char *) &n)[1])
28-
# define ntoh_l(n) (u_long)(((u_char *)&n)[0] << 24 | \
29-
((u_char *)&n)[1] << 16 | \
30-
((u_char *)&n)[2] << 8 | ((u_char *)&n)[3])
31-
# define hton_s(n) (u_short)(((u_char *) &n)[2] << 8 | ((u_char *) &n)[3])
28+
# define ntoh_s(n) (u_short)(((u_char *)&n)[1] << 8 \
29+
| ((u_char *)&n)[0])
30+
# define ntoh_l(n) (u_long) (((u_char *)&n)[3] << 24 \
31+
| ((u_char *)&n)[2] << 16 \
32+
| ((u_char *)&n)[1] << 8 \
33+
| ((u_char *)&n)[0])
34+
# define hton_s(n) (ntoh_s(n))
3235
# define hton_l(n) (ntoh_l(n))
3336
# else /* BYTE_ORDER != BIG_ENDIAN */
3437
# if BYTE_ORDER == PDP_ENDIAN
@@ -43,9 +46,10 @@
4346
int pqPutShort(int integer, FILE *f)
4447
{
4548
int retval = 0;
46-
u_short n;
49+
u_short n,s;
4750

48-
n = hton_s(integer);
51+
s = integer;
52+
n = hton_s(s);
4953
if(fwrite(&n, sizeof(u_short), 1, f) != 1)
5054
retval = EOF;
5155

0 commit comments

Comments
 (0)