Fix bug in do_query. origin/V4_5_STABLE
authorTatsuo Ishii <ishii@postgresql.org>
Fri, 15 Nov 2024 08:08:26 +0000 (17:08 +0900)
committerTatsuo Ishii <ishii@postgresql.org>
Fri, 15 Nov 2024 08:56:24 +0000 (17:56 +0900)
Upon receiving DataRow packet, it converts the number of fields from
network byte order to host byte order.  Unfortunately it used htons()
for this purpose instead of ntohs(). This is simply wrong. Similarly it
used htonl() instead of htohl() while converting the data length from
network byte order to host byte order. This is wrong too. But
fortunately both ntohs()/htons() and ntohl()/htonl() swap the byte and
they bring the same result (i.e. htonl(data_len) ==
ntohl(data_len). So actually the bug does not hurt anything.
However a bug is a bug. This commit fixes them.

Backpatch-through: v4.1

src/protocol/pool_process_query.c

index a17475ea2ea99f5802fb5830b82d29663e72fadb..15b0f41f0e42a7a1446c814149951b7865170b4f 100644 (file)
@@ -2356,7 +2356,7 @@ do_query(POOL_CONNECTION * backend, char *query, POOL_SELECT_RESULT * *result, i
                                        if (p)
                                        {
                                                memcpy(&shortval, p, sizeof(short));
-                                               num_fields = htons(shortval);
+                                               num_fields = ntohs(shortval);
                                                p += sizeof(short);
                                        }
                                        else
@@ -2388,7 +2388,7 @@ do_query(POOL_CONNECTION * backend, char *query, POOL_SELECT_RESULT * *result, i
                                                if (major == PROTO_MAJOR_V3)
                                                {
                                                        memcpy(&intval, p, sizeof(int));
-                                                       len = htonl(intval);
+                                                       len = ntohl(intval);
                                                        p += sizeof(int);
 
                                                        res->nullflags[num_data] = len;