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

Commit 0507977

Browse files
committed
Introduce pg_attribute_nonnull(...)
pg_attribute_nonnull(...) can be used to generate compiler warnings when a function is called with the specified arguments set to NULL, as per an idea from Andres Freund. An empty argument list indicates that no pointer arguments can be NULL. pg_attribute_nonnull() only works for compilers that support the nonnull function attribute. If nonnull is not supported, pg_attribute_nonnull() has no effect. As a beginning, this commit uses it for the DefineCustomXXXVariable() functions to generate warnings when the "name" and "value" arguments are set to NULL. This will likely be expanded to other places in the future, where it makes sense. Author: Nathan Bossart Reviewed by: Michael Paquier, Tom Lane Discussion: https://postgr.es/m/20220525061739.ur7x535vtzyzkmqo@alap3.anarazel.de
1 parent c99c67f commit 0507977

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/include/c.h

+11
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@
144144
#define pg_attribute_no_sanitize_alignment()
145145
#endif
146146

147+
/*
148+
* pg_attribute_nonnull means the compiler should warn if the function is
149+
* called with the listed arguments set to NULL. If no arguments are
150+
* listed, the compiler should warn if any pointer arguments are set to NULL.
151+
*/
152+
#if __has_attribute (nonnull)
153+
#define pg_attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
154+
#else
155+
#define pg_attribute_nonnull(...)
156+
#endif
157+
147158
/*
148159
* Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
149160
* used in assert-enabled builds, to avoid compiler warnings about unused

src/include/utils/guc.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ extern void DefineCustomBoolVariable(const char *name,
307307
int flags,
308308
GucBoolCheckHook check_hook,
309309
GucBoolAssignHook assign_hook,
310-
GucShowHook show_hook);
310+
GucShowHook show_hook) pg_attribute_nonnull(1, 4);
311311

312312
extern void DefineCustomIntVariable(const char *name,
313313
const char *short_desc,
@@ -320,7 +320,7 @@ extern void DefineCustomIntVariable(const char *name,
320320
int flags,
321321
GucIntCheckHook check_hook,
322322
GucIntAssignHook assign_hook,
323-
GucShowHook show_hook);
323+
GucShowHook show_hook) pg_attribute_nonnull(1, 4);
324324

325325
extern void DefineCustomRealVariable(const char *name,
326326
const char *short_desc,
@@ -333,7 +333,7 @@ extern void DefineCustomRealVariable(const char *name,
333333
int flags,
334334
GucRealCheckHook check_hook,
335335
GucRealAssignHook assign_hook,
336-
GucShowHook show_hook);
336+
GucShowHook show_hook) pg_attribute_nonnull(1, 4);
337337

338338
extern void DefineCustomStringVariable(const char *name,
339339
const char *short_desc,
@@ -344,7 +344,7 @@ extern void DefineCustomStringVariable(const char *name,
344344
int flags,
345345
GucStringCheckHook check_hook,
346346
GucStringAssignHook assign_hook,
347-
GucShowHook show_hook);
347+
GucShowHook show_hook) pg_attribute_nonnull(1, 4);
348348

349349
extern void DefineCustomEnumVariable(const char *name,
350350
const char *short_desc,
@@ -356,7 +356,7 @@ extern void DefineCustomEnumVariable(const char *name,
356356
int flags,
357357
GucEnumCheckHook check_hook,
358358
GucEnumAssignHook assign_hook,
359-
GucShowHook show_hook);
359+
GucShowHook show_hook) pg_attribute_nonnull(1, 4);
360360

361361
extern void MarkGUCPrefixReserved(const char *className);
362362

0 commit comments

Comments
 (0)