Convert *GetDatum() and DatumGet*() macros to inline functions

From: Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Convert *GetDatum() and DatumGet*() macros to inline functions
Date: 2022-08-28 15:55:15
Message-ID: 8528fb7e-0aa2-6b54-85fb-0c0886dbd6ed@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


I once wrote code like this:

char *oid = get_from_somewhere();
...

values[i++] = ObjectIdGetDatum(oid);

This compiles cleanly and even appears to work in practice, except of
course it doesn't.

The FooGetDatum() macros just cast whatever you give it to Datum,
without checking whether the input was really foo.

To address this, I converted these macros to inline functions, which
enables type checking of the input argument. For symmetry, I also
converted the corresponding DatumGetFoo() macros (but those are less
likely to cover mistakes, since the input argument is always Datum).
This is patch 0002.

(I left some of the DatumGet... of the varlena types in fmgr.h as
macros. These ultimately map to functions that do type checking, so
there would be little more to be learnt from that. But we could do
those for consistency as well.)

This whole thing threw up a bunch of compiler warnings and errors, which
revealed a number of existing misuses. These are fixed in patch 0001.
These include

- using FooGetDatum on things that are already Datum,

- using DatumGetPointer on things that are already pointers,

- using PG_RETURN_TYPE on things that are Datum,

- using PG_RETURN_TYPE of the wrong type,

and others, including my personal favorite:

- using PointerGetDatum where DatumGetPointer should be used.

(AFAICT, unlike my initial example, I don't think any of those would
cause wrong behavior.)

Attachment Content-Type Size
0001-Fix-incorrect-uses-of-Datum-conversion-macros.patch text/plain 15.0 KB
0002-Convert-GetDatum-and-DatumGet-macros-to-inline-funct.patch text/plain 39.9 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2022-08-28 16:07:52 Re: CI and test improvements
Previous Message Tom Lane 2022-08-28 15:51:19 Re: [PATCH] Add native windows on arm64 support