aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2008-11-10 14:57:38 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2008-11-10 14:57:38 +0000
commitd141e7493bb821300396d155fdea62a0278ddd6a (patch)
tree05c2918231d499fd8b5fe998a13b907ecba4069c
parentc5451c22e38bc3044588c596966afcbe0c29b103 (diff)
downloadpostgresql-d141e7493bb821300396d155fdea62a0278ddd6a.tar.gz
postgresql-d141e7493bb821300396d155fdea62a0278ddd6a.zip
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.c30
1 files changed, 12 insertions, 18 deletions
diff --git a/contrib/sslinfo/sslinfo.c b/contrib/sslinfo/sslinfo.c
index a215abb8b6e..a8145653c0c 100644
--- a/contrib/sslinfo/sslinfo.c
+++ b/contrib/sslinfo/sslinfo.c
@@ -4,7 +4,7 @@
* Written by Victor B. Wagner <vitus@cryptocom.ru>, Cryptocom LTD
* 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"
@@ -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);
}