Skip to content

Commit 587de22

Browse files
committed
Add missing error handling in pg_md5_hash().
It failed to provide an error string as expected for the admittedly-unlikely case of OOM in pg_cryptohash_create(). Also, make it initialize *errstr to NULL for success, as pg_md5_binary() does. Also add missing comments. Readers should not have to reverse-engineer the API spec for a publicly visible routine.
1 parent 36d4efe commit 587de22

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/common/md5_common.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ bytesToHex(uint8 b[16], char *s)
5757
* characters. you thus need to provide an array
5858
* of 33 characters, including the trailing '\0'.
5959
*
60+
* errstr filled with a constant-string error message
61+
* on failure return; NULL on success.
62+
*
6063
* RETURNS false on failure (out of memory for internal buffers
6164
* or MD5 computation failure) or true on success.
6265
*
@@ -72,9 +75,13 @@ pg_md5_hash(const void *buff, size_t len, char *hexsum, const char **errstr)
7275
uint8 sum[MD5_DIGEST_LENGTH];
7376
pg_cryptohash_ctx *ctx;
7477

78+
*errstr = NULL;
7579
ctx = pg_cryptohash_create(PG_MD5);
7680
if (ctx == NULL)
81+
{
82+
*errstr = pg_cryptohash_error(NULL); /* returns OOM */
7783
return false;
84+
}
7885

7986
if (pg_cryptohash_init(ctx) < 0 ||
8087
pg_cryptohash_update(ctx, buff, len) < 0 ||
@@ -90,6 +97,12 @@ pg_md5_hash(const void *buff, size_t len, char *hexsum, const char **errstr)
9097
return true;
9198
}
9299

100+
/*
101+
* pg_md5_binary
102+
*
103+
* As above, except that the MD5 digest is returned as a binary string
104+
* (of size MD5_DIGEST_LENGTH) rather than being converted to ASCII hex.
105+
*/
93106
bool
94107
pg_md5_binary(const void *buff, size_t len, void *outbuf, const char **errstr)
95108
{

0 commit comments

Comments
 (0)