Fix memory leaks in px_crypt_shacrypt().
authorTom Lane <[email protected]>
Sun, 6 Apr 2025 15:57:22 +0000 (11:57 -0400)
committerTom Lane <[email protected]>
Sun, 6 Apr 2025 15:57:22 +0000 (11:57 -0400)
Per Coverity.  I don't think these are of any actual significance
since the function ought to be invoked in a short-lived context.
Still, if it's trying to be neat it should get it right.

Also const-ify a constant and fix up typedef formatting.

contrib/pgcrypto/crypt-sha.c
src/tools/pgindent/typedefs.list

index 862f3fb9a68b773fca4458a95eced5c3e2b73614..7e9440ca78439afb963ff4e61175deaed0210d25 100644 (file)
@@ -56,7 +56,7 @@ typedef enum
    PGCRYPTO_SHA256CRYPT = 0,
    PGCRYPTO_SHA512CRYPT = 1,
    PGCRYPTO_SHA_UNKOWN
-}          PGCRYPTO_SHA_t;
+} PGCRYPTO_SHA_t;
 
 static unsigned char _crypt_itoa64[64 + 1] =
 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
@@ -68,7 +68,7 @@ char *
 px_crypt_shacrypt(const char *pw, const char *salt, char *passwd, unsigned dstlen)
 {
    static const char rounds_prefix[] = "rounds=";
-   static char *magic_bytes[2] = {"$5$", "$6$"};
+   static const char *magic_bytes[2] = {"$5$", "$6$"};
 
    /* Used to create the password hash string */
    StringInfo  out_buf = NULL;
@@ -96,10 +96,6 @@ px_crypt_shacrypt(const char *pw, const char *salt, char *passwd, unsigned dstle
    unsigned int len,
                salt_len = 0;
 
-   /* Init result buffer */
-   out_buf = makeStringInfoExt(PX_SHACRYPT_BUF_LEN);
-   decoded_salt = makeStringInfoExt(PX_SHACRYPT_SALT_MAX_LEN);
-
    /* Sanity checks */
    if (!passwd)
        return NULL;
@@ -116,6 +112,10 @@ px_crypt_shacrypt(const char *pw, const char *salt, char *passwd, unsigned dstle
    if (dstlen < PX_SHACRYPT_BUF_LEN)
        elog(ERROR, "insufficient result buffer size to encrypt password");
 
+   /* Init result buffer */
+   out_buf = makeStringInfoExt(PX_SHACRYPT_BUF_LEN);
+   decoded_salt = makeStringInfoExt(PX_SHACRYPT_SALT_MAX_LEN);
+
    /* Init contents of buffers properly */
    memset(&sha_buf, '\0', sizeof(sha_buf));
    memset(&sha_buf_tmp, '\0', sizeof(sha_buf_tmp));
@@ -630,8 +630,8 @@ error:
    if (digestB != NULL)
        px_md_free(digestB);
 
-   if (out_buf != NULL)
-       destroyStringInfo(out_buf);
+   destroyStringInfo(out_buf);
+   destroyStringInfo(decoded_salt);
 
    ereport(ERROR,
            errcode(ERRCODE_INTERNAL_ERROR),
index 1a30437ad969f82462f6968f68c63d89ddf79c01..d42b943ef943f34911b0b0bbc259e66648e66d1d 100644 (file)
@@ -1825,6 +1825,7 @@ PGAlignedBlock
 PGAlignedXLogBlock
 PGAsyncStatusType
 PGCALL2
+PGCRYPTO_SHA_t
 PGChecksummablePage
 PGContextVisibility
 PGErrorVerbosity