1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-29 10:41:53 +03:00

Add use of asprintf()

Add asprintf(), pg_asprintf(), and psprintf() to simplify string
allocation and composition.  Replacement implementations taken from
NetBSD.

Reviewed-by: Álvaro Herrera <alvherre@2ndquadrant.com>
Reviewed-by: Asif Naeem <anaeem.it@gmail.com>
This commit is contained in:
Peter Eisentraut
2013-10-13 00:09:18 -04:00
parent a53dee43fe
commit 5b6d08cd29
47 changed files with 253 additions and 363 deletions

View File

@ -127,12 +127,9 @@ main(void)
{
for (j = 0; times[j]; j++)
{
int length = strlen(dates[i])
+ 1
+ strlen(times[j])
+ 1;
char* t = malloc(length);
sprintf(t, "%s %s", dates[i], times[j]);
char* t;
if (asprintf(&t, "%s %s", dates[i], times[j]) < 0)
abort();
ts1 = PGTYPEStimestamp_from_asc(t, NULL);
text = PGTYPEStimestamp_to_asc(ts1);
if (i != 19 || j != 3) /* timestamp as integer or double differ for this case */

View File

@ -92,12 +92,9 @@ main(void)
{
for (j = 0; times[j]; j++)
{
int length = strlen(dates[i])
+ 1
+ strlen(times[j])
+ 1;
char* t = malloc(length);
sprintf(t, "%s %s", dates[i], times[j]);
char* t;
if (asprintf(&t, "%s %s", dates[i], times[j]) < 0)
abort();
ts1 = PGTYPEStimestamp_from_asc(t, NULL);
text = PGTYPEStimestamp_to_asc(ts1);
if (i != 19 || j != 3) /* timestamp as integer or double differ for this case */

View File

@ -420,7 +420,6 @@ pg_GSS_startup(PGconn *conn)
{
OM_uint32 maj_stat,
min_stat;
int maxlen;
gss_buffer_desc temp_gbuf;
if (!(conn->pghost && conn->pghost[0] != '\0'))
@ -441,10 +440,14 @@ pg_GSS_startup(PGconn *conn)
* Import service principal name so the proper ticket can be acquired by
* the GSSAPI system.
*/
maxlen = NI_MAXHOST + strlen(conn->krbsrvname) + 2;
temp_gbuf.value = (char *) malloc(maxlen);
snprintf(temp_gbuf.value, maxlen, "%s@%s",
conn->krbsrvname, conn->pghost);
if (asprintf((char **)&temp_gbuf.value, "%s@%s",
conn->krbsrvname, conn->pghost) < 0)
{
printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("out of memory"));
return STATUS_ERROR;
}
temp_gbuf.length = strlen(temp_gbuf.value);
maj_stat = gss_import_name(&min_stat, &temp_gbuf,
@ -656,13 +659,11 @@ pg_SSPI_startup(PGconn *conn, int use_negotiate)
libpq_gettext("host name must be specified\n"));
return STATUS_ERROR;
}
conn->sspitarget = malloc(strlen(conn->krbsrvname) + strlen(conn->pghost) + 2);
if (!conn->sspitarget)
if (asprintf(&conn->sspitarget, "%s/%s", conn->krbsrvname, conn->pghost) < 0)
{
printfPQExpBuffer(&conn->errorMessage, libpq_gettext("out of memory\n"));
return STATUS_ERROR;
}
sprintf(conn->sspitarget, "%s/%s", conn->krbsrvname, conn->pghost);
/*
* Indicate that we're in SSPI authentication mode to make sure that