summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2008-11-10 14:57:53 +0000
committerTom Lane2008-11-10 14:57:53 +0000
commitac2cd02c2a235028615283beead9e39c8ee7d708 (patch)
tree289c2f8017c92038c4038a864df59112e4c47920
parent371706471ee67977ad0bd9afba6cd7ffa1c5db6b (diff)
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.
-rw-r--r--contrib/sslinfo/sslinfo.c28
1 files changed, 11 insertions, 17 deletions
diff --git a/contrib/sslinfo/sslinfo.c b/contrib/sslinfo/sslinfo.c
index 7bf08855ad..8482cfab9d 100644
--- a/contrib/sslinfo/sslinfo.c
+++ b/contrib/sslinfo/sslinfo.c
@@ -113,9 +113,9 @@ ssl_client_serial(PG_FUNCTION_ARGS)
Datum
ASN1_STRING_to_text(ASN1_STRING *str)
{
- BIO *membuf = NULL;
- size_t size,
- outlen;
+ BIO *membuf;
+ size_t size;
+ char nullterm;
char *sp;
char *dp;
text *result;
@@ -125,16 +125,15 @@ ASN1_STRING_to_text(ASN1_STRING *str)
ASN1_STRING_print_ex(membuf, str,
((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB)
| ASN1_STRFLGS_UTF8_CONVERT));
-
- outlen = 0;
- BIO_write(membuf, &outlen, 1);
+ /* ensure null termination of the BIO's content */
+ nullterm = '\0';
+ BIO_write(membuf, &nullterm, 1);
size = BIO_get_mem_data(membuf, &sp);
dp = (char *) pg_do_encoding_conversion((unsigned char *) sp,
size - 1,
PG_UTF8,
GetDatabaseEncoding());
result = cstring_to_text(dp);
-
if (dp != sp)
pfree(dp);
BIO_free(membuf);
@@ -271,6 +270,7 @@ X509_NAME_to_text(X509_NAME *name)
ASN1_STRING *v;
const char *field_name;
size_t size;
+ char nullterm;
char *sp;
char *dp;
text *result;
@@ -290,24 +290,18 @@ X509_NAME_to_text(X509_NAME *name)
| ASN1_STRFLGS_UTF8_CONVERT));
}
- i = 0;
- BIO_write(membuf, &i, 1);
+ /* ensure null termination of the BIO's content */
+ nullterm = '\0';
+ BIO_write(membuf, &nullterm, 1);
size = BIO_get_mem_data(membuf, &sp);
-
dp = (char *) pg_do_encoding_conversion((unsigned char *) sp,
size - 1,
PG_UTF8,
GetDatabaseEncoding());
- BIO_free(membuf);
-
result = cstring_to_text(dp);
-
- /*
- * pg_do_encoding_conversion has annoying habit of returning source
- * pointer
- */
if (dp != sp)
pfree(dp);
+ BIO_free(membuf);
PG_RETURN_TEXT_P(result);
}