1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-21 05:21:08 +03:00

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/61be9e31-7b7d-49d5-bc11-721800d89d64@eisentraut.org
This commit is contained in:
Heikki Linnakangas
2025-05-08 22:01:25 +03:00
parent 965213d9c5
commit b28c59a6cd
24 changed files with 80 additions and 80 deletions

View File

@@ -41,15 +41,15 @@ static const int8 b64lookup[128] = {
/*
* pg_b64_encode
*
* Encode into base64 the given string. Returns the length of the encoded
* string, and -1 in the event of an error with the result buffer zeroed
* for safety.
* Encode the 'src' byte array into base64. Returns the length of the encoded
* string, and -1 in the event of an error with the result buffer zeroed for
* safety.
*/
int
pg_b64_encode(const char *src, int len, char *dst, int dstlen)
pg_b64_encode(const uint8 *src, int len, char *dst, int dstlen)
{
char *p;
const char *s,
const uint8 *s,
*end = src + len;
int pos = 2;
uint32 buf = 0;
@@ -59,7 +59,7 @@ pg_b64_encode(const char *src, int len, char *dst, int dstlen)
while (s < end)
{
buf |= (unsigned char) *s << (pos << 3);
buf |= *s << (pos << 3);
pos--;
s++;
@@ -113,11 +113,11 @@ error:
* buffer zeroed for safety.
*/
int
pg_b64_decode(const char *src, int len, char *dst, int dstlen)
pg_b64_decode(const char *src, int len, uint8 *dst, int dstlen)
{
const char *srcend = src + len,
*s = src;
char *p = dst;
uint8 *p = dst;
char c;
int b = 0;
uint32 buf = 0;

View File

@@ -105,7 +105,7 @@ pg_md5_hash(const void *buff, size_t len, char *hexsum, const char **errstr)
* (of size MD5_DIGEST_LENGTH) rather than being converted to ASCII hex.
*/
bool
pg_md5_binary(const void *buff, size_t len, void *outbuf, const char **errstr)
pg_md5_binary(const void *buff, size_t len, uint8 *outbuf, const char **errstr)
{
pg_cryptohash_ctx *ctx;
@@ -142,7 +142,7 @@ pg_md5_binary(const void *buff, size_t len, void *outbuf, const char **errstr)
* error context.
*/
bool
pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len,
pg_md5_encrypt(const char *passwd, const uint8 *salt, size_t salt_len,
char *buf, const char **errstr)
{
size_t passwd_len = strlen(passwd);

View File

@@ -37,7 +37,7 @@
int
scram_SaltedPassword(const char *password,
pg_cryptohash_type hash_type, int key_length,
const char *salt, int saltlen, int iterations,
const uint8 *salt, int saltlen, int iterations,
uint8 *result, const char **errstr)
{
int password_len = strlen(password);
@@ -62,7 +62,7 @@ scram_SaltedPassword(const char *password,
/* First iteration */
if (pg_hmac_init(hmac_ctx, (uint8 *) password, password_len) < 0 ||
pg_hmac_update(hmac_ctx, (uint8 *) salt, saltlen) < 0 ||
pg_hmac_update(hmac_ctx, salt, saltlen) < 0 ||
pg_hmac_update(hmac_ctx, (uint8 *) &one, sizeof(uint32)) < 0 ||
pg_hmac_final(hmac_ctx, Ui_prev, key_length) < 0)
{
@@ -207,7 +207,7 @@ scram_ServerKey(const uint8 *salted_password,
*/
char *
scram_build_secret(pg_cryptohash_type hash_type, int key_length,
const char *salt, int saltlen, int iterations,
const uint8 *salt, int saltlen, int iterations,
const char *password, const char **errstr)
{
uint8 salted_password[SCRAM_MAX_KEY_LEN];
@@ -290,7 +290,7 @@ scram_build_secret(pg_cryptohash_type hash_type, int key_length,
*(p++) = '$';
/* stored key */
encoded_result = pg_b64_encode((char *) stored_key, key_length, p,
encoded_result = pg_b64_encode(stored_key, key_length, p,
encoded_stored_len);
if (encoded_result < 0)
{
@@ -307,7 +307,7 @@ scram_build_secret(pg_cryptohash_type hash_type, int key_length,
*(p++) = ':';
/* server key */
encoded_result = pg_b64_encode((char *) server_key, key_length, p,
encoded_result = pg_b64_encode(server_key, key_length, p,
encoded_server_len);
if (encoded_result < 0)
{