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

Commit 4212179

Browse files
committed
Change pqformat.h's integer handling functions to take unsigned integers.
As added in 1de09ad the new functions all accept signed integers as parameters. That's not great, because it's perfectly reasonable to call them with unsigned parameters. Unfortunately unsigned to signed conversion is not well defined, when exceeding the range of the signed value. That's presently not a practical issue in postgres (among other reasons because we force gcc's hand with -fwrapv). But it's clearly not quite right. Thus change the signatures to accept unsigned integers instead, signed to unsigned conversion is always well defined. Also change the backward compat pq_sendint() - while it's deprecated it seems better to be consistent. Per discussion between Andrew Gierth, Michael Paquier, Alvaro Herrera and Tom Lane. Reported-By: Andrew Gierth Author: Andres Freund Discussion: https://postgr.es/m/87r2m10zm2.fsf@news-spur.riddles.org.uk
1 parent 9860708 commit 4212179

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

src/include/libpq/pqformat.h

+41-41
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extern void pq_sendfloat4(StringInfo buf, float4 f);
3535
extern void pq_sendfloat8(StringInfo buf, float8 f);
3636

3737
/*
38-
* Append an int8 to a StringInfo buffer, which already has enough space
38+
* Append a [u]int8 to a StringInfo buffer, which already has enough space
3939
* preallocated.
4040
*
4141
* The use of pg_restrict allows the compiler to optimize the code based on
@@ -47,55 +47,55 @@ extern void pq_sendfloat8(StringInfo buf, float8 f);
4747
* overly picky and demanding a * before a restrict.
4848
*/
4949
static inline void
50-
pq_writeint8(StringInfoData *pg_restrict buf, int8 i)
50+
pq_writeint8(StringInfoData *pg_restrict buf, uint8 i)
5151
{
52-
int8 ni = i;
52+
uint8 ni = i;
5353

54-
Assert(buf->len + (int) sizeof(int8) <= buf->maxlen);
55-
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(int8));
56-
buf->len += sizeof(int8);
54+
Assert(buf->len + (int) sizeof(uint8) <= buf->maxlen);
55+
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(uint8));
56+
buf->len += sizeof(uint8);
5757
}
5858

5959
/*
60-
* Append an int16 to a StringInfo buffer, which already has enough space
60+
* Append a [u]int16 to a StringInfo buffer, which already has enough space
6161
* preallocated.
6262
*/
6363
static inline void
64-
pq_writeint16(StringInfoData *pg_restrict buf, int16 i)
64+
pq_writeint16(StringInfoData *pg_restrict buf, uint16 i)
6565
{
66-
int16 ni = pg_hton16(i);
66+
uint16 ni = pg_hton16(i);
6767

68-
Assert(buf->len + (int) sizeof(int16) <= buf->maxlen);
69-
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(int16));
70-
buf->len += sizeof(int16);
68+
Assert(buf->len + (int) sizeof(uint16) <= buf->maxlen);
69+
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(uint16));
70+
buf->len += sizeof(uint16);
7171
}
7272

7373
/*
74-
* Append an int32 to a StringInfo buffer, which already has enough space
74+
* Append a [u]int32 to a StringInfo buffer, which already has enough space
7575
* preallocated.
7676
*/
7777
static inline void
78-
pq_writeint32(StringInfoData *pg_restrict buf, int32 i)
78+
pq_writeint32(StringInfoData *pg_restrict buf, uint32 i)
7979
{
80-
int32 ni = pg_hton32(i);
80+
uint32 ni = pg_hton32(i);
8181

82-
Assert(buf->len + (int) sizeof(int32) <= buf->maxlen);
83-
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(int32));
84-
buf->len += sizeof(int32);
82+
Assert(buf->len + (int) sizeof(uint32) <= buf->maxlen);
83+
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(uint32));
84+
buf->len += sizeof(uint32);
8585
}
8686

8787
/*
88-
* Append an int64 to a StringInfo buffer, which already has enough space
88+
* Append a [u]int64 to a StringInfo buffer, which already has enough space
8989
* preallocated.
9090
*/
9191
static inline void
92-
pq_writeint64(StringInfoData *pg_restrict buf, int64 i)
92+
pq_writeint64(StringInfoData *pg_restrict buf, uint64 i)
9393
{
94-
int64 ni = pg_hton64(i);
94+
uint64 ni = pg_hton64(i);
9595

96-
Assert(buf->len + (int) sizeof(int64) <= buf->maxlen);
97-
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(int64));
98-
buf->len += sizeof(int64);
96+
Assert(buf->len + (int) sizeof(uint64) <= buf->maxlen);
97+
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(uint64));
98+
buf->len += sizeof(uint64);
9999
}
100100

101101
/*
@@ -127,41 +127,41 @@ pq_writestring(StringInfoData *pg_restrict buf, const char *pg_restrict str)
127127
pfree(p);
128128
}
129129

130-
/* append a binary int8 to a StringInfo buffer */
130+
/* append a binary [u]int8 to a StringInfo buffer */
131131
static inline void
132-
pq_sendint8(StringInfo buf, int8 i)
132+
pq_sendint8(StringInfo buf, uint8 i)
133133
{
134-
enlargeStringInfo(buf, sizeof(int8));
134+
enlargeStringInfo(buf, sizeof(uint8));
135135
pq_writeint8(buf, i);
136136
}
137137

138-
/* append a binary int16 to a StringInfo buffer */
138+
/* append a binary [u]int16 to a StringInfo buffer */
139139
static inline void
140-
pq_sendint16(StringInfo buf, int16 i)
140+
pq_sendint16(StringInfo buf, uint16 i)
141141
{
142-
enlargeStringInfo(buf, sizeof(int16));
142+
enlargeStringInfo(buf, sizeof(uint16));
143143
pq_writeint16(buf, i);
144144
}
145145

146-
/* append a binary int32 to a StringInfo buffer */
146+
/* append a binary [u]int32 to a StringInfo buffer */
147147
static inline void
148-
pq_sendint32(StringInfo buf, int32 i)
148+
pq_sendint32(StringInfo buf, uint32 i)
149149
{
150-
enlargeStringInfo(buf, sizeof(int32));
150+
enlargeStringInfo(buf, sizeof(uint32));
151151
pq_writeint32(buf, i);
152152
}
153153

154-
/* append a binary int64 to a StringInfo buffer */
154+
/* append a binary [u]int64 to a StringInfo buffer */
155155
static inline void
156-
pq_sendint64(StringInfo buf, int64 i)
156+
pq_sendint64(StringInfo buf, uint64 i)
157157
{
158-
enlargeStringInfo(buf, sizeof(int64));
158+
enlargeStringInfo(buf, sizeof(uint64));
159159
pq_writeint64(buf, i);
160160
}
161161

162162
/* append a binary byte to a StringInfo buffer */
163163
static inline void
164-
pq_sendbyte(StringInfo buf, int8 byt)
164+
pq_sendbyte(StringInfo buf, uint8 byt)
165165
{
166166
pq_sendint8(buf, byt);
167167
}
@@ -172,18 +172,18 @@ pq_sendbyte(StringInfo buf, int8 byt)
172172
* This function is deprecated; prefer use of the functions above.
173173
*/
174174
static inline void
175-
pq_sendint(StringInfo buf, int i, int b)
175+
pq_sendint(StringInfo buf, uint32 i, int b)
176176
{
177177
switch (b)
178178
{
179179
case 1:
180-
pq_sendint8(buf, (int8) i);
180+
pq_sendint8(buf, (uint8) i);
181181
break;
182182
case 2:
183-
pq_sendint16(buf, (int16) i);
183+
pq_sendint16(buf, (uint16) i);
184184
break;
185185
case 4:
186-
pq_sendint32(buf, (int32) i);
186+
pq_sendint32(buf, (uint32) i);
187187
break;
188188
default:
189189
elog(ERROR, "unsupported integer size %d", b);

0 commit comments

Comments
 (0)