Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b28c59a

Browse files
committed
Use 'void *' for arbitrary buffers, 'uint8 *' for byte arrays
A 'void *' argument suggests that the caller might pass an arbitrary struct, which is appropriate for functions like libc's read/write, or pq_sendbytes(). 'uint8 *' is more appropriate for byte arrays that have no structure, like the cancellation keys or SCRAM tokens. Some places used 'char *', but 'uint8 *' is better because 'char *' is commonly used for null-terminated strings. Change code around SCRAM, MD5 authentication, and cancellation key handling to follow these conventions. Discussion: https://www.postgresql.org/message-id/[email protected]
1 parent 965213d commit b28c59a

File tree

24 files changed

+80
-80
lines changed

24 files changed

+80
-80
lines changed

contrib/dblink/dblink.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3218,7 +3218,7 @@ appendSCRAMKeysInfo(StringInfo buf)
32183218
len = pg_b64_enc_len(sizeof(MyProcPort->scram_ClientKey));
32193219
/* don't forget the zero-terminator */
32203220
client_key = palloc0(len + 1);
3221-
encoded_len = pg_b64_encode((const char *) MyProcPort->scram_ClientKey,
3221+
encoded_len = pg_b64_encode(MyProcPort->scram_ClientKey,
32223222
sizeof(MyProcPort->scram_ClientKey),
32233223
client_key, len);
32243224
if (encoded_len < 0)
@@ -3227,7 +3227,7 @@ appendSCRAMKeysInfo(StringInfo buf)
32273227
len = pg_b64_enc_len(sizeof(MyProcPort->scram_ServerKey));
32283228
/* don't forget the zero-terminator */
32293229
server_key = palloc0(len + 1);
3230-
encoded_len = pg_b64_encode((const char *) MyProcPort->scram_ServerKey,
3230+
encoded_len = pg_b64_encode(MyProcPort->scram_ServerKey,
32313231
sizeof(MyProcPort->scram_ServerKey),
32323232
server_key, len);
32333233
if (encoded_len < 0)

contrib/postgres_fdw/connection.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ connect_pg_server(ForeignServer *server, UserMapping *user)
577577
len = pg_b64_enc_len(sizeof(MyProcPort->scram_ClientKey));
578578
/* don't forget the zero-terminator */
579579
values[n] = palloc0(len + 1);
580-
encoded_len = pg_b64_encode((const char *) MyProcPort->scram_ClientKey,
580+
encoded_len = pg_b64_encode(MyProcPort->scram_ClientKey,
581581
sizeof(MyProcPort->scram_ClientKey),
582582
(char *) values[n], len);
583583
if (encoded_len < 0)
@@ -588,7 +588,7 @@ connect_pg_server(ForeignServer *server, UserMapping *user)
588588
len = pg_b64_enc_len(sizeof(MyProcPort->scram_ServerKey));
589589
/* don't forget the zero-terminator */
590590
values[n] = palloc0(len + 1);
591-
encoded_len = pg_b64_encode((const char *) MyProcPort->scram_ServerKey,
591+
encoded_len = pg_b64_encode(MyProcPort->scram_ServerKey,
592592
sizeof(MyProcPort->scram_ServerKey),
593593
(char *) values[n], len);
594594
if (encoded_len < 0)

src/backend/libpq/auth-scram.c

+16-16
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ typedef struct
158158
/* Fields from the last message from client */
159159
char *client_final_message_without_proof;
160160
char *client_final_nonce;
161-
char ClientProof[SCRAM_MAX_KEY_LEN];
161+
uint8 ClientProof[SCRAM_MAX_KEY_LEN];
162162

163163
/* Fields generated in the server */
164164
char *server_first_message;
@@ -186,9 +186,9 @@ static void mock_scram_secret(const char *username, pg_cryptohash_type *hash_typ
186186
static bool is_scram_printable(char *p);
187187
static char *sanitize_char(char c);
188188
static char *sanitize_str(const char *s);
189-
static char *scram_mock_salt(const char *username,
190-
pg_cryptohash_type hash_type,
191-
int key_length);
189+
static uint8 *scram_mock_salt(const char *username,
190+
pg_cryptohash_type hash_type,
191+
int key_length);
192192

193193
/*
194194
* The number of iterations to use when generating new secrets.
@@ -484,7 +484,7 @@ pg_be_scram_build_secret(const char *password)
484484
{
485485
char *prep_password;
486486
pg_saslprep_rc rc;
487-
char saltbuf[SCRAM_DEFAULT_SALT_LEN];
487+
uint8 saltbuf[SCRAM_DEFAULT_SALT_LEN];
488488
char *result;
489489
const char *errstr = NULL;
490490

@@ -524,7 +524,7 @@ scram_verify_plain_password(const char *username, const char *password,
524524
const char *secret)
525525
{
526526
char *encoded_salt;
527-
char *salt;
527+
uint8 *salt;
528528
int saltlen;
529529
int iterations;
530530
int key_length = 0;
@@ -609,9 +609,9 @@ parse_scram_secret(const char *secret, int *iterations,
609609
char *storedkey_str;
610610
char *serverkey_str;
611611
int decoded_len;
612-
char *decoded_salt_buf;
613-
char *decoded_stored_buf;
614-
char *decoded_server_buf;
612+
uint8 *decoded_salt_buf;
613+
uint8 *decoded_stored_buf;
614+
uint8 *decoded_server_buf;
615615

616616
/*
617617
* The secret is of form:
@@ -698,7 +698,7 @@ mock_scram_secret(const char *username, pg_cryptohash_type *hash_type,
698698
int *iterations, int *key_length, char **salt,
699699
uint8 *stored_key, uint8 *server_key)
700700
{
701-
char *raw_salt;
701+
uint8 *raw_salt;
702702
char *encoded_salt;
703703
int encoded_len;
704704

@@ -1231,7 +1231,7 @@ build_server_first_message(scram_state *state)
12311231
* For convenience, however, we don't use the whole range available,
12321232
* rather, we generate some random bytes, and base64 encode them.
12331233
*/
1234-
char raw_nonce[SCRAM_RAW_NONCE_LEN];
1234+
uint8 raw_nonce[SCRAM_RAW_NONCE_LEN];
12351235
int encoded_len;
12361236

12371237
if (!pg_strong_random(raw_nonce, SCRAM_RAW_NONCE_LEN))
@@ -1271,7 +1271,7 @@ read_client_final_message(scram_state *state, const char *input)
12711271
char *begin,
12721272
*proof;
12731273
char *p;
1274-
char *client_proof;
1274+
uint8 *client_proof;
12751275
int client_proof_len;
12761276

12771277
begin = p = pstrdup(input);
@@ -1340,7 +1340,7 @@ read_client_final_message(scram_state *state, const char *input)
13401340
b64_message_len = pg_b64_enc_len(cbind_input_len);
13411341
/* don't forget the zero-terminator */
13421342
b64_message = palloc(b64_message_len + 1);
1343-
b64_message_len = pg_b64_encode(cbind_input, cbind_input_len,
1343+
b64_message_len = pg_b64_encode((uint8 *) cbind_input, cbind_input_len,
13441344
b64_message, b64_message_len);
13451345
if (b64_message_len < 0)
13461346
elog(ERROR, "could not encode channel binding data");
@@ -1440,7 +1440,7 @@ build_server_final_message(scram_state *state)
14401440
siglen = pg_b64_enc_len(state->key_length);
14411441
/* don't forget the zero-terminator */
14421442
server_signature_base64 = palloc(siglen + 1);
1443-
siglen = pg_b64_encode((const char *) ServerSignature,
1443+
siglen = pg_b64_encode(ServerSignature,
14441444
state->key_length, server_signature_base64,
14451445
siglen);
14461446
if (siglen < 0)
@@ -1467,7 +1467,7 @@ build_server_final_message(scram_state *state)
14671467
* hash based on the username and a cluster-level secret key. Returns a
14681468
* pointer to a static buffer of size SCRAM_DEFAULT_SALT_LEN, or NULL.
14691469
*/
1470-
static char *
1470+
static uint8 *
14711471
scram_mock_salt(const char *username, pg_cryptohash_type hash_type,
14721472
int key_length)
14731473
{
@@ -1501,5 +1501,5 @@ scram_mock_salt(const char *username, pg_cryptohash_type hash_type,
15011501
}
15021502
pg_cryptohash_free(ctx);
15031503

1504-
return (char *) sha_digest;
1504+
return sha_digest;
15051505
}

src/backend/libpq/auth.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ ClientAuthentication(Port *port)
666666
* Send an authentication request packet to the frontend.
667667
*/
668668
void
669-
sendAuthRequest(Port *port, AuthRequest areq, const char *extradata, int extralen)
669+
sendAuthRequest(Port *port, AuthRequest areq, const void *extradata, int extralen)
670670
{
671671
StringInfoData buf;
672672

@@ -874,7 +874,7 @@ CheckPWChallengeAuth(Port *port, const char **logdetail)
874874
static int
875875
CheckMD5Auth(Port *port, char *shadow_pass, const char **logdetail)
876876
{
877-
char md5Salt[4]; /* Password salt */
877+
uint8 md5Salt[4]; /* Password salt */
878878
char *passwd;
879879
int result;
880880

src/backend/libpq/crypt.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ encrypt_password(PasswordType target_type, const char *role,
136136
case PASSWORD_TYPE_MD5:
137137
encrypted_password = palloc(MD5_PASSWD_LEN + 1);
138138

139-
if (!pg_md5_encrypt(password, role, strlen(role),
139+
if (!pg_md5_encrypt(password, (uint8 *) role, strlen(role),
140140
encrypted_password, &errstr))
141141
elog(ERROR, "password encryption failed: %s", errstr);
142142
break;
@@ -201,7 +201,7 @@ encrypt_password(PasswordType target_type, const char *role,
201201
int
202202
md5_crypt_verify(const char *role, const char *shadow_pass,
203203
const char *client_pass,
204-
const char *md5_salt, int md5_salt_len,
204+
const uint8 *md5_salt, int md5_salt_len,
205205
const char **logdetail)
206206
{
207207
int retval;
@@ -284,7 +284,7 @@ plain_crypt_verify(const char *role, const char *shadow_pass,
284284

285285
case PASSWORD_TYPE_MD5:
286286
if (!pg_md5_encrypt(client_pass,
287-
role,
287+
(uint8 *) role,
288288
strlen(role),
289289
crypt_client_pass,
290290
&errstr))

src/backend/storage/ipc/procsignal.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ typedef struct
6464
{
6565
pg_atomic_uint32 pss_pid;
6666
int pss_cancel_key_len; /* 0 means no cancellation is possible */
67-
char pss_cancel_key[MAX_CANCEL_KEY_LENGTH];
67+
uint8 pss_cancel_key[MAX_CANCEL_KEY_LENGTH];
6868
volatile sig_atomic_t pss_signalFlags[NUM_PROCSIGNALS];
6969
slock_t pss_mutex; /* protects the above fields */
7070

@@ -163,7 +163,7 @@ ProcSignalShmemInit(void)
163163
* Register the current process in the ProcSignal array
164164
*/
165165
void
166-
ProcSignalInit(char *cancel_key, int cancel_key_len)
166+
ProcSignalInit(const uint8 *cancel_key, int cancel_key_len)
167167
{
168168
ProcSignalSlot *slot;
169169
uint64 barrier_generation;
@@ -729,7 +729,7 @@ procsignal_sigusr1_handler(SIGNAL_ARGS)
729729
* fields in the ProcSignal slots.
730730
*/
731731
void
732-
SendCancelRequest(int backendPID, char *cancel_key, int cancel_key_len)
732+
SendCancelRequest(int backendPID, const uint8 *cancel_key, int cancel_key_len)
733733
{
734734
Assert(backendPID != 0);
735735

src/backend/utils/init/globals.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pg_time_t MyStartTime;
5050
TimestampTz MyStartTimestamp;
5151
struct ClientSocket *MyClientSocket;
5252
struct Port *MyProcPort;
53-
char MyCancelKey[MAX_CANCEL_KEY_LENGTH];
53+
uint8 MyCancelKey[MAX_CANCEL_KEY_LENGTH];
5454
int MyCancelKeyLength = 0;
5555
int MyPMChildSlot;
5656

src/common/base64.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ static const int8 b64lookup[128] = {
4141
/*
4242
* pg_b64_encode
4343
*
44-
* Encode into base64 the given string. Returns the length of the encoded
45-
* string, and -1 in the event of an error with the result buffer zeroed
46-
* for safety.
44+
* Encode the 'src' byte array into base64. Returns the length of the encoded
45+
* string, and -1 in the event of an error with the result buffer zeroed for
46+
* safety.
4747
*/
4848
int
49-
pg_b64_encode(const char *src, int len, char *dst, int dstlen)
49+
pg_b64_encode(const uint8 *src, int len, char *dst, int dstlen)
5050
{
5151
char *p;
52-
const char *s,
52+
const uint8 *s,
5353
*end = src + len;
5454
int pos = 2;
5555
uint32 buf = 0;
@@ -59,7 +59,7 @@ pg_b64_encode(const char *src, int len, char *dst, int dstlen)
5959

6060
while (s < end)
6161
{
62-
buf |= (unsigned char) *s << (pos << 3);
62+
buf |= *s << (pos << 3);
6363
pos--;
6464
s++;
6565

@@ -113,11 +113,11 @@ pg_b64_encode(const char *src, int len, char *dst, int dstlen)
113113
* buffer zeroed for safety.
114114
*/
115115
int
116-
pg_b64_decode(const char *src, int len, char *dst, int dstlen)
116+
pg_b64_decode(const char *src, int len, uint8 *dst, int dstlen)
117117
{
118118
const char *srcend = src + len,
119119
*s = src;
120-
char *p = dst;
120+
uint8 *p = dst;
121121
char c;
122122
int b = 0;
123123
uint32 buf = 0;

src/common/md5_common.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pg_md5_hash(const void *buff, size_t len, char *hexsum, const char **errstr)
105105
* (of size MD5_DIGEST_LENGTH) rather than being converted to ASCII hex.
106106
*/
107107
bool
108-
pg_md5_binary(const void *buff, size_t len, void *outbuf, const char **errstr)
108+
pg_md5_binary(const void *buff, size_t len, uint8 *outbuf, const char **errstr)
109109
{
110110
pg_cryptohash_ctx *ctx;
111111

@@ -142,7 +142,7 @@ pg_md5_binary(const void *buff, size_t len, void *outbuf, const char **errstr)
142142
* error context.
143143
*/
144144
bool
145-
pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len,
145+
pg_md5_encrypt(const char *passwd, const uint8 *salt, size_t salt_len,
146146
char *buf, const char **errstr)
147147
{
148148
size_t passwd_len = strlen(passwd);

src/common/scram-common.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
int
3838
scram_SaltedPassword(const char *password,
3939
pg_cryptohash_type hash_type, int key_length,
40-
const char *salt, int saltlen, int iterations,
40+
const uint8 *salt, int saltlen, int iterations,
4141
uint8 *result, const char **errstr)
4242
{
4343
int password_len = strlen(password);
@@ -62,7 +62,7 @@ scram_SaltedPassword(const char *password,
6262

6363
/* First iteration */
6464
if (pg_hmac_init(hmac_ctx, (uint8 *) password, password_len) < 0 ||
65-
pg_hmac_update(hmac_ctx, (uint8 *) salt, saltlen) < 0 ||
65+
pg_hmac_update(hmac_ctx, salt, saltlen) < 0 ||
6666
pg_hmac_update(hmac_ctx, (uint8 *) &one, sizeof(uint32)) < 0 ||
6767
pg_hmac_final(hmac_ctx, Ui_prev, key_length) < 0)
6868
{
@@ -207,7 +207,7 @@ scram_ServerKey(const uint8 *salted_password,
207207
*/
208208
char *
209209
scram_build_secret(pg_cryptohash_type hash_type, int key_length,
210-
const char *salt, int saltlen, int iterations,
210+
const uint8 *salt, int saltlen, int iterations,
211211
const char *password, const char **errstr)
212212
{
213213
uint8 salted_password[SCRAM_MAX_KEY_LEN];
@@ -290,7 +290,7 @@ scram_build_secret(pg_cryptohash_type hash_type, int key_length,
290290
*(p++) = '$';
291291

292292
/* stored key */
293-
encoded_result = pg_b64_encode((char *) stored_key, key_length, p,
293+
encoded_result = pg_b64_encode(stored_key, key_length, p,
294294
encoded_stored_len);
295295
if (encoded_result < 0)
296296
{
@@ -307,7 +307,7 @@ scram_build_secret(pg_cryptohash_type hash_type, int key_length,
307307
*(p++) = ':';
308308

309309
/* server key */
310-
encoded_result = pg_b64_encode((char *) server_key, key_length, p,
310+
encoded_result = pg_b64_encode(server_key, key_length, p,
311311
encoded_server_len);
312312
if (encoded_result < 0)
313313
{

src/include/common/base64.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#define BASE64_H
1212

1313
/* base 64 */
14-
pg_nodiscard extern int pg_b64_encode(const char *src, int len, char *dst, int dstlen);
15-
pg_nodiscard extern int pg_b64_decode(const char *src, int len, char *dst, int dstlen);
14+
pg_nodiscard extern int pg_b64_encode(const uint8 *src, int len, char *dst, int dstlen);
15+
pg_nodiscard extern int pg_b64_decode(const char *src, int len, uint8 *dst, int dstlen);
1616
extern int pg_b64_enc_len(int srclen);
1717
extern int pg_b64_dec_len(int srclen);
1818

src/include/common/md5.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
/* Utilities common to all the MD5 implementations, as of md5_common.c */
2929
extern bool pg_md5_hash(const void *buff, size_t len, char *hexsum,
3030
const char **errstr);
31-
extern bool pg_md5_binary(const void *buff, size_t len, void *outbuf,
31+
extern bool pg_md5_binary(const void *buff, size_t len, uint8 *outbuf,
3232
const char **errstr);
33-
extern bool pg_md5_encrypt(const char *passwd, const char *salt,
33+
extern bool pg_md5_encrypt(const char *passwd, const uint8 *salt,
3434
size_t salt_len, char *buf,
3535
const char **errstr);
3636

src/include/common/scram-common.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
extern int scram_SaltedPassword(const char *password,
5353
pg_cryptohash_type hash_type, int key_length,
54-
const char *salt, int saltlen, int iterations,
54+
const uint8 *salt, int saltlen, int iterations,
5555
uint8 *result, const char **errstr);
5656
extern int scram_H(const uint8 *input, pg_cryptohash_type hash_type,
5757
int key_length, uint8 *result,
@@ -64,7 +64,7 @@ extern int scram_ServerKey(const uint8 *salted_password,
6464
uint8 *result, const char **errstr);
6565

6666
extern char *scram_build_secret(pg_cryptohash_type hash_type, int key_length,
67-
const char *salt, int saltlen, int iterations,
67+
const uint8 *salt, int saltlen, int iterations,
6868
const char *password, const char **errstr);
6969

7070
#endif /* SCRAM_COMMON_H */

src/include/libpq/auth.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extern PGDLLIMPORT bool pg_krb_caseins_users;
3737
extern PGDLLIMPORT bool pg_gss_accept_delegation;
3838

3939
extern void ClientAuthentication(Port *port);
40-
extern void sendAuthRequest(Port *port, AuthRequest areq, const char *extradata,
40+
extern void sendAuthRequest(Port *port, AuthRequest areq, const void *extradata,
4141
int extralen);
4242
extern void set_authn_id(Port *port, const char *id);
4343

src/include/libpq/crypt.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extern char *encrypt_password(PasswordType target_type, const char *role,
5151
extern char *get_role_password(const char *role, const char **logdetail);
5252

5353
extern int md5_crypt_verify(const char *role, const char *shadow_pass,
54-
const char *client_pass, const char *md5_salt,
54+
const char *client_pass, const uint8 *md5_salt,
5555
int md5_salt_len, const char **logdetail);
5656
extern int plain_crypt_verify(const char *role, const char *shadow_pass,
5757
const char *client_pass,

0 commit comments

Comments
 (0)