Thread

  1. [PATCH v2 3/3] Separate read and write pointers in pg_saslprep

    Bertrand Drouvot <bertranddrouvot.pg@gmail.com> — 2025-12-18T12:17:21Z

    Use separate pointers for reading const input ('p') and writing
    to mutable output ('outp'), avoiding the need to cast away const
    on the input parameter.
    
    Discussion: https://postgr.es/m/aUQHy/MmWq7c97wK%40ip-10-97-1-34.eu-west-3.compute.internal
    ---
     src/common/saslprep.c | 15 ++++++++-------
     1 file changed, 8 insertions(+), 7 deletions(-)
     100.0% src/common/
    
    diff --git a/src/common/saslprep.c b/src/common/saslprep.c
    index 101e8d65a4d..b215ab0bf8e 100644
    --- a/src/common/saslprep.c
    +++ b/src/common/saslprep.c
    @@ -1054,7 +1054,8 @@ pg_saslprep(const char *input, char **output)
     	int			count;
     	int			i;
     	bool		contains_RandALCat;
    -	unsigned char *p;
    +	const unsigned char *p;
    +	unsigned char *outp;
     	char32_t   *wp;
     
     	/* Ensure we return *output as NULL on failure */
    @@ -1087,7 +1088,7 @@ pg_saslprep(const char *input, char **output)
     	if (!input_chars)
     		goto oom;
     
    -	p = (unsigned char *) input;
    +	p = (const unsigned char *) input;
     	for (i = 0; i < input_size; i++)
     	{
     		input_chars[i] = utf8_to_unicode(p);
    @@ -1217,14 +1218,14 @@ pg_saslprep(const char *input, char **output)
     	 * There are no error exits below here, so the error exit paths don't need
     	 * to worry about possibly freeing "result".
     	 */
    -	p = (unsigned char *) result;
    +	outp = (unsigned char *) result;
     	for (wp = output_chars; *wp; wp++)
     	{
    -		unicode_to_utf8(*wp, p);
    -		p += pg_utf_mblen(p);
    +		unicode_to_utf8(*wp, outp);
    +		outp += pg_utf_mblen(outp);
     	}
    -	Assert((char *) p == result + result_size);
    -	*p = '\0';
    +	Assert((char *) outp == result + result_size);
    +	*outp = '\0';
     
     	FREE(input_chars);
     	FREE(output_chars);
    -- 
    2.34.1
    
    
    --IBFQ0J+7bhRCocF6--