mirror of
https://github.com/postgres/postgres.git
synced 2025-10-22 14:32:25 +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:
@@ -70,14 +70,14 @@ typedef struct
|
||||
|
||||
/* These come from the server-first message */
|
||||
char *server_first_message;
|
||||
char *salt;
|
||||
uint8 *salt;
|
||||
int saltlen;
|
||||
int iterations;
|
||||
char *nonce;
|
||||
|
||||
/* These come from the server-final message */
|
||||
char *server_final_message;
|
||||
char ServerSignature[SCRAM_MAX_KEY_LEN];
|
||||
uint8 ServerSignature[SCRAM_MAX_KEY_LEN];
|
||||
} fe_scram_state;
|
||||
|
||||
static bool read_server_first_message(fe_scram_state *state, char *input);
|
||||
@@ -350,7 +350,7 @@ static char *
|
||||
build_client_first_message(fe_scram_state *state)
|
||||
{
|
||||
PGconn *conn = state->conn;
|
||||
char raw_nonce[SCRAM_RAW_NONCE_LEN + 1];
|
||||
uint8 raw_nonce[SCRAM_RAW_NONCE_LEN + 1];
|
||||
char *result;
|
||||
int channel_info_len;
|
||||
int encoded_len;
|
||||
@@ -513,7 +513,7 @@ build_client_final_message(fe_scram_state *state)
|
||||
free(cbind_input);
|
||||
goto oom_error;
|
||||
}
|
||||
encoded_cbind_len = pg_b64_encode(cbind_input, cbind_input_len,
|
||||
encoded_cbind_len = pg_b64_encode((uint8 *) cbind_input, cbind_input_len,
|
||||
buf.data + buf.len,
|
||||
encoded_cbind_len);
|
||||
if (encoded_cbind_len < 0)
|
||||
@@ -574,7 +574,7 @@ build_client_final_message(fe_scram_state *state)
|
||||
encoded_len = pg_b64_enc_len(state->key_length);
|
||||
if (!enlargePQExpBuffer(&buf, encoded_len))
|
||||
goto oom_error;
|
||||
encoded_len = pg_b64_encode((char *) client_proof,
|
||||
encoded_len = pg_b64_encode(client_proof,
|
||||
state->key_length,
|
||||
buf.data + buf.len,
|
||||
encoded_len);
|
||||
@@ -694,7 +694,7 @@ read_server_final_message(fe_scram_state *state, char *input)
|
||||
{
|
||||
PGconn *conn = state->conn;
|
||||
char *encoded_server_signature;
|
||||
char *decoded_server_signature;
|
||||
uint8 *decoded_server_signature;
|
||||
int server_signature_len;
|
||||
|
||||
state->server_final_message = strdup(input);
|
||||
@@ -916,7 +916,7 @@ pg_fe_scram_build_secret(const char *password, int iterations, const char **errs
|
||||
{
|
||||
char *prep_password;
|
||||
pg_saslprep_rc rc;
|
||||
char saltbuf[SCRAM_DEFAULT_SALT_LEN];
|
||||
uint8 saltbuf[SCRAM_DEFAULT_SALT_LEN];
|
||||
char *result;
|
||||
|
||||
/*
|
||||
|
@@ -798,7 +798,7 @@ pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
|
||||
int ret;
|
||||
char *crypt_pwd = NULL;
|
||||
const char *pwd_to_send;
|
||||
char md5Salt[4];
|
||||
uint8 md5Salt[4];
|
||||
|
||||
/* Read the salt from the AuthenticationMD5Password message. */
|
||||
if (areq == AUTH_REQ_MD5)
|
||||
@@ -829,7 +829,7 @@ pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
|
||||
}
|
||||
|
||||
crypt_pwd2 = crypt_pwd + MD5_PASSWD_LEN + 1;
|
||||
if (!pg_md5_encrypt(password, conn->pguser,
|
||||
if (!pg_md5_encrypt(password, (uint8 *) conn->pguser,
|
||||
strlen(conn->pguser), crypt_pwd2,
|
||||
&errstr))
|
||||
{
|
||||
@@ -1369,7 +1369,7 @@ PQencryptPassword(const char *passwd, const char *user)
|
||||
if (!crypt_pwd)
|
||||
return NULL;
|
||||
|
||||
if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd, &errstr))
|
||||
if (!pg_md5_encrypt(passwd, (uint8 *) user, strlen(user), crypt_pwd, &errstr))
|
||||
{
|
||||
free(crypt_pwd);
|
||||
return NULL;
|
||||
@@ -1482,7 +1482,7 @@ PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user,
|
||||
{
|
||||
const char *errstr = NULL;
|
||||
|
||||
if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd, &errstr))
|
||||
if (!pg_md5_encrypt(passwd, (uint8 *) user, strlen(user), crypt_pwd, &errstr))
|
||||
{
|
||||
libpq_append_conn_error(conn, "could not encrypt password: %s", errstr);
|
||||
free(crypt_pwd);
|
||||
|
@@ -463,7 +463,7 @@ PQsendCancelRequest(PGconn *cancelConn)
|
||||
memset(&req, 0, offsetof(CancelRequestPacket, cancelAuthCode));
|
||||
req.cancelRequestCode = (MsgType) pg_hton32(CANCEL_REQUEST_CODE);
|
||||
req.backendPID = pg_hton32(cancelConn->be_pid);
|
||||
if (pqPutnchar((char *) &req, offsetof(CancelRequestPacket, cancelAuthCode), cancelConn))
|
||||
if (pqPutnchar(&req, offsetof(CancelRequestPacket, cancelAuthCode), cancelConn))
|
||||
return STATUS_ERROR;
|
||||
if (pqPutnchar(cancelConn->be_cancel_key, cancelConn->be_cancel_key_len, cancelConn))
|
||||
return STATUS_ERROR;
|
||||
|
@@ -67,7 +67,7 @@ PQlibVersion(void)
|
||||
|
||||
|
||||
/*
|
||||
* pqGetc: get 1 character from the connection
|
||||
* pqGetc: read 1 character from the connection
|
||||
*
|
||||
* All these routines return 0 on success, EOF on error.
|
||||
* Note that for the Get routines, EOF only means there is not enough
|
||||
@@ -100,7 +100,7 @@ pqPutc(char c, PGconn *conn)
|
||||
|
||||
/*
|
||||
* pqGets[_append]:
|
||||
* get a null-terminated string from the connection,
|
||||
* read a null-terminated string from the connection,
|
||||
* and store it in an expansible PQExpBuffer.
|
||||
* If we run out of memory, all of the string is still read,
|
||||
* but the excess characters are silently discarded.
|
||||
@@ -159,10 +159,10 @@ pqPuts(const char *s, PGconn *conn)
|
||||
|
||||
/*
|
||||
* pqGetnchar:
|
||||
* get a string of exactly len bytes in buffer s, no null termination
|
||||
* read exactly len bytes in buffer s, no null termination
|
||||
*/
|
||||
int
|
||||
pqGetnchar(char *s, size_t len, PGconn *conn)
|
||||
pqGetnchar(void *s, size_t len, PGconn *conn)
|
||||
{
|
||||
if (len > (size_t) (conn->inEnd - conn->inCursor))
|
||||
return EOF;
|
||||
@@ -199,7 +199,7 @@ pqSkipnchar(size_t len, PGconn *conn)
|
||||
* write exactly len bytes to the current message
|
||||
*/
|
||||
int
|
||||
pqPutnchar(const char *s, size_t len, PGconn *conn)
|
||||
pqPutnchar(const void *s, size_t len, PGconn *conn)
|
||||
{
|
||||
if (pqPutMsgBytes(s, len, conn))
|
||||
return EOF;
|
||||
|
@@ -2121,7 +2121,7 @@ pqFunctionCall3(PGconn *conn, Oid fnid,
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pqPutnchar((char *) args[i].u.ptr, args[i].len, conn))
|
||||
if (pqPutnchar(args[i].u.ptr, args[i].len, conn))
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -2215,7 +2215,7 @@ pqFunctionCall3(PGconn *conn, Oid fnid,
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pqGetnchar((char *) result_buf,
|
||||
if (pqGetnchar(result_buf,
|
||||
*actual_result_len,
|
||||
conn))
|
||||
continue;
|
||||
|
@@ -539,16 +539,16 @@ struct pg_conn
|
||||
* tried host */
|
||||
bool send_appname; /* okay to send application_name? */
|
||||
size_t scram_client_key_len;
|
||||
void *scram_client_key_binary; /* binary SCRAM client key */
|
||||
uint8 *scram_client_key_binary; /* binary SCRAM client key */
|
||||
size_t scram_server_key_len;
|
||||
void *scram_server_key_binary; /* binary SCRAM server key */
|
||||
uint8 *scram_server_key_binary; /* binary SCRAM server key */
|
||||
ProtocolVersion min_pversion; /* protocol version to request */
|
||||
ProtocolVersion max_pversion; /* protocol version to request */
|
||||
|
||||
/* Miscellaneous stuff */
|
||||
int be_pid; /* PID of backend --- needed for cancels */
|
||||
int be_cancel_key_len;
|
||||
char *be_cancel_key; /* query cancellation key and its length */
|
||||
uint8 *be_cancel_key; /* query cancellation key */
|
||||
pgParameterStatus *pstatus; /* ParameterStatus data */
|
||||
int client_encoding; /* encoding id */
|
||||
bool std_strings; /* standard_conforming_strings */
|
||||
@@ -787,9 +787,9 @@ extern int pqPutc(char c, PGconn *conn);
|
||||
extern int pqGets(PQExpBuffer buf, PGconn *conn);
|
||||
extern int pqGets_append(PQExpBuffer buf, PGconn *conn);
|
||||
extern int pqPuts(const char *s, PGconn *conn);
|
||||
extern int pqGetnchar(char *s, size_t len, PGconn *conn);
|
||||
extern int pqGetnchar(void *s, size_t len, PGconn *conn);
|
||||
extern int pqSkipnchar(size_t len, PGconn *conn);
|
||||
extern int pqPutnchar(const char *s, size_t len, PGconn *conn);
|
||||
extern int pqPutnchar(const void *s, size_t len, PGconn *conn);
|
||||
extern int pqGetInt(int *result, size_t bytes, PGconn *conn);
|
||||
extern int pqPutInt(int value, size_t bytes, PGconn *conn);
|
||||
extern int pqPutMsgStart(char msg_type, PGconn *conn);
|
||||
|
Reference in New Issue
Block a user