Skip to content

Commit

Permalink
Extend PgQueryProtobuf.len to machine word size
Browse files Browse the repository at this point in the history
The len field is expected to be size_t from the protobuf side (see the
definition of pg_query__parse_result__unpack()). Also, clients are
expecting to be able to use size_t-like types when filling the
PgQueryProtobuf structure.

This is how this problem was discovered in pglast:
deparse_protobuf(parse_sql_protobuf('select 1')) would always return ''
on big-endian machines.

deparse_protobuf() populated a 64-bit machine-word-sized len field, but
libpg-query would only read an "int" from it, which is 32-bit only even
on 64-bit machines. On big-endian machines, the "wrong" half of the
integer is read, so the len is effectively always zeros.

Close lelit/pglast#114.
  • Loading branch information
df7cb authored and msepga committed Jan 11, 2023
1 parent 1ada550 commit 20b3179
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int main() {
} else {
scan_result = pg_query__scan_result__unpack(NULL, result.pbuf.len, (void *) result.pbuf.data);

printf(" version: %d, tokens: %ld, size: %d\n", scan_result->version, scan_result->n_tokens, result.pbuf.len);
printf(" version: %d, tokens: %ld, size: %zu\n", scan_result->version, scan_result->n_tokens, result.pbuf.len);
for (j = 0; j < scan_result->n_tokens; j++) {
scan_token = scan_result->tokens[j];
token_kind = protobuf_c_enum_descriptor_get_value(&pg_query__token__descriptor, scan_token->token);
Expand Down
3 changes: 2 additions & 1 deletion pg_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define PG_QUERY_H

#include <stdint.h>
#include <sys/types.h>

typedef struct {
char* message; // exception message
Expand All @@ -13,7 +14,7 @@ typedef struct {
} PgQueryError;

typedef struct {
unsigned int len;
size_t len;
char* data;
} PgQueryProtobuf;

Expand Down

0 comments on commit 20b3179

Please sign in to comment.