Skip to content

Commit c196842

Browse files
committed
Refactor hba_authname
The previous implementation (from 9afffcb) had an unnecessary check on the boundaries of the enum which trigtered compile warnings. To clean it up, move the pre-existing static assert to a central location and call that. Reported-By: Erik Rijkers Reviewed-By: Michael Paquier Discussion: https://postgr.es/m/[email protected]
1 parent 4560e0a commit c196842

File tree

3 files changed

+9
-20
lines changed

3 files changed

+9
-20
lines changed

src/backend/libpq/auth.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ set_authn_id(Port *port, const char *id)
379379
ereport(LOG,
380380
errmsg("connection authenticated: identity=\"%s\" method=%s "
381381
"(%s:%d)",
382-
port->authn_id, hba_authname(port), HbaFileName,
382+
port->authn_id, hba_authname(port->hba->auth_method), HbaFileName,
383383
port->hba->linenumber));
384384
}
385385
}

src/backend/libpq/hba.c

+7-18
Original file line numberDiff line numberDiff line change
@@ -2607,14 +2607,8 @@ fill_hba_line(Tuplestorestate *tuple_store, TupleDesc tupdesc,
26072607
else
26082608
nulls[index++] = true;
26092609

2610-
/*
2611-
* Make sure UserAuthName[] tracks additions to the UserAuth enum
2612-
*/
2613-
StaticAssertStmt(lengthof(UserAuthName) == USER_AUTH_LAST + 1,
2614-
"UserAuthName[] must match the UserAuth enum");
2615-
26162610
/* auth_method */
2617-
values[index++] = CStringGetTextDatum(UserAuthName[hba->auth_method]);
2611+
values[index++] = CStringGetTextDatum(hba_authname(hba->auth_method));
26182612

26192613
/* options */
26202614
options = gethba_options(hba);
@@ -3150,18 +3144,13 @@ hba_getauthmethod(hbaPort *port)
31503144
* should not be freed.
31513145
*/
31523146
const char *
3153-
hba_authname(hbaPort *port)
3147+
hba_authname(UserAuth auth_method)
31543148
{
3155-
UserAuth auth_method;
3156-
3157-
Assert(port->hba);
3158-
auth_method = port->hba->auth_method;
3159-
3160-
if (auth_method < 0 || USER_AUTH_LAST < auth_method)
3161-
{
3162-
/* Should never happen. */
3163-
elog(FATAL, "port has out-of-bounds UserAuth: %d", auth_method);
3164-
}
3149+
/*
3150+
* Make sure UserAuthName[] tracks additions to the UserAuth enum
3151+
*/
3152+
StaticAssertStmt(lengthof(UserAuthName) == USER_AUTH_LAST + 1,
3153+
"UserAuthName[] must match the UserAuth enum");
31653154

31663155
return UserAuthName[auth_method];
31673156
}

src/include/libpq/hba.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ typedef struct Port hbaPort;
137137

138138
extern bool load_hba(void);
139139
extern bool load_ident(void);
140-
extern const char *hba_authname(hbaPort *port);
140+
extern const char *hba_authname(UserAuth auth_method);
141141
extern void hba_getauthmethod(hbaPort *port);
142142
extern int check_usermap(const char *usermap_name,
143143
const char *pg_role, const char *auth_user,

0 commit comments

Comments
 (0)