Commit d141e749 authored by Tom Lane's avatar Tom Lane

Fix old bug in contrib/sslinfo: X509_NAME_to_text freed the BIO_s_mem buffer

it was using too soon.  In a situation where pg_do_encoding_conversion is
a no-op, this led to garbage data returned.

In HEAD, also modify the code that's ensuring null termination to make it
a tad more obvious what's happening.
parent c5451c22
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* Written by Victor B. Wagner <vitus@cryptocom.ru>, Cryptocom LTD * Written by Victor B. Wagner <vitus@cryptocom.ru>, Cryptocom LTD
* This file is distributed under BSD-style license. * This file is distributed under BSD-style license.
* *
* $PostgreSQL: pgsql/contrib/sslinfo/sslinfo.c,v 1.7 2008/03/25 22:42:42 tgl Exp $ * $PostgreSQL: pgsql/contrib/sslinfo/sslinfo.c,v 1.8 2008/11/10 14:57:38 tgl Exp $
*/ */
#include "postgres.h" #include "postgres.h"
...@@ -113,9 +113,9 @@ ssl_client_serial(PG_FUNCTION_ARGS) ...@@ -113,9 +113,9 @@ ssl_client_serial(PG_FUNCTION_ARGS)
Datum Datum
ASN1_STRING_to_text(ASN1_STRING *str) ASN1_STRING_to_text(ASN1_STRING *str)
{ {
BIO *membuf = NULL; BIO *membuf;
size_t size, size_t size;
outlen; char nullterm;
char *sp; char *sp;
char *dp; char *dp;
text *result; text *result;
...@@ -125,16 +125,15 @@ ASN1_STRING_to_text(ASN1_STRING *str) ...@@ -125,16 +125,15 @@ ASN1_STRING_to_text(ASN1_STRING *str)
ASN1_STRING_print_ex(membuf, str, ASN1_STRING_print_ex(membuf, str,
((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB) ((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB)
| ASN1_STRFLGS_UTF8_CONVERT)); | ASN1_STRFLGS_UTF8_CONVERT));
/* ensure null termination of the BIO's content */
outlen = 0; nullterm = '\0';
BIO_write(membuf, &outlen, 1); BIO_write(membuf, &nullterm, 1);
size = BIO_get_mem_data(membuf, &sp); size = BIO_get_mem_data(membuf, &sp);
dp = (char *) pg_do_encoding_conversion((unsigned char *) sp, dp = (char *) pg_do_encoding_conversion((unsigned char *) sp,
size - 1, size - 1,
PG_UTF8, PG_UTF8,
GetDatabaseEncoding()); GetDatabaseEncoding());
result = cstring_to_text(dp); result = cstring_to_text(dp);
if (dp != sp) if (dp != sp)
pfree(dp); pfree(dp);
BIO_free(membuf); BIO_free(membuf);
...@@ -271,6 +270,7 @@ X509_NAME_to_text(X509_NAME *name) ...@@ -271,6 +270,7 @@ X509_NAME_to_text(X509_NAME *name)
ASN1_STRING *v; ASN1_STRING *v;
const char *field_name; const char *field_name;
size_t size; size_t size;
char nullterm;
char *sp; char *sp;
char *dp; char *dp;
text *result; text *result;
...@@ -290,24 +290,18 @@ X509_NAME_to_text(X509_NAME *name) ...@@ -290,24 +290,18 @@ X509_NAME_to_text(X509_NAME *name)
| ASN1_STRFLGS_UTF8_CONVERT)); | ASN1_STRFLGS_UTF8_CONVERT));
} }
i = 0; /* ensure null termination of the BIO's content */
BIO_write(membuf, &i, 1); nullterm = '\0';
BIO_write(membuf, &nullterm, 1);
size = BIO_get_mem_data(membuf, &sp); size = BIO_get_mem_data(membuf, &sp);
dp = (char *) pg_do_encoding_conversion((unsigned char *) sp, dp = (char *) pg_do_encoding_conversion((unsigned char *) sp,
size - 1, size - 1,
PG_UTF8, PG_UTF8,
GetDatabaseEncoding()); GetDatabaseEncoding());
BIO_free(membuf);
result = cstring_to_text(dp); result = cstring_to_text(dp);
/*
* pg_do_encoding_conversion has annoying habit of returning source
* pointer
*/
if (dp != sp) if (dp != sp)
pfree(dp); pfree(dp);
BIO_free(membuf);
PG_RETURN_TEXT_P(result); PG_RETURN_TEXT_P(result);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment