Changeset: b168a9bbea12 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/b168a9bbea12 Modified Files: clients/mapilib/msettings.c clients/mapilib/msettings.h clients/odbc/driver/SQLConnect.c Branch: odbc_loader Log Message:
Change msetting_as_string API to never allocate diffs (100 lines): diff --git a/clients/mapilib/msettings.c b/clients/mapilib/msettings.c --- a/clients/mapilib/msettings.c +++ b/clients/mapilib/msettings.c @@ -503,27 +503,22 @@ msetting_parse(msettings *mp, mparm parm } } -char * -msetting_as_string(const msettings *mp, mparm parm) +const char * +msetting_as_string(const msettings *mp, mparm parm, char *scratch, size_t scratch_size) { - bool b; long l; - const char *s; switch (mparm_classify(parm)) { case MPCLASS_BOOL: - b = msetting_bool(mp, parm); - return strdup(b ? "true" : "false"); + return msetting_bool(mp, parm) ? "true" : "false"; case MPCLASS_LONG: l = msetting_long(mp, parm); - int n = 40; - char *buf = malloc(n); - if (!buf) + int n = snprintf(scratch, scratch_size, "%ld", l); + if (n > 0 && scratch_size >= (size_t)n + 1) + return scratch; + else return NULL; - snprintf(buf, n, "%ld", l); - return buf; case MPCLASS_STRING: - s = msetting_string(mp, parm); - return strdup(s); + return msetting_string(mp, parm); default: assert(0 && "unreachable"); return NULL; diff --git a/clients/mapilib/msettings.h b/clients/mapilib/msettings.h --- a/clients/mapilib/msettings.h +++ b/clients/mapilib/msettings.h @@ -154,9 +154,11 @@ mapi_export msettings_error msetting_set mapi_export bool msetting_bool(const msettings *mp, mparm parm); mapi_export msettings_error msetting_set_bool(msettings *mp, mparm parm, bool value); -/* parse into the appropriate type, or format into newly malloc'ed string (NULL means malloc failed) */ +/* Parse into the appropriate type */ mapi_export msettings_error msetting_parse(msettings *mp, mparm parm, const char *text); -mapi_export char *msetting_as_string(const msettings *mp, mparm parm); +/* Render setting as a string, requires a small scratch buffer (40 bytes is fine) for rendering integers. + * Changing the msettings or the scratch buffer makes the returned pointer invalid. */ +mapi_export const char *msetting_as_string(const msettings *mp, mparm parm, char *scratch, size_t scratch_size); /* store named parameter */ mapi_export msettings_error msetting_set_named(msettings *mp, bool allow_core, const char *key, const char *value); diff --git a/clients/odbc/driver/SQLConnect.c b/clients/odbc/driver/SQLConnect.c --- a/clients/odbc/driver/SQLConnect.c +++ b/clients/odbc/driver/SQLConnect.c @@ -141,8 +141,9 @@ buildConnectionString(const char *dsn, c size_t cap = 1024; char *buf = malloc(cap); // reallocprintf will deal with allocation failures char *sep = ""; - char *value = NULL; - char *default_value = NULL; + const char *value = NULL; + const char *default_value = NULL; + char scratch1[40], scratch2[40]; bool ok = false; if (dsn) { @@ -158,8 +159,7 @@ buildConnectionString(const char *dsn, c if (parm == MP_IGNORE || parm == MP_TABLE || parm == MP_TABLESCHEMA) continue; - free(value); - value = msetting_as_string(settings, parm); + value = msetting_as_string(settings, parm, scratch1, sizeof(scratch1)); if (!value) goto end; @@ -174,8 +174,7 @@ buildConnectionString(const char *dsn, c show_this = true; } else { // skip if still default - free(default_value); - default_value = msetting_as_string(msettings_default, parm); + default_value = msetting_as_string(msettings_default, parm, scratch2, sizeof(scratch2)); if (!default_value) goto end; show_this = (strcmp(value, default_value) != 0); @@ -190,8 +189,6 @@ buildConnectionString(const char *dsn, c ok = true; end: - free(value); - free(default_value); if (ok) { return buf; } else { _______________________________________________ checkin-list mailing list -- checkin-list@monetdb.org To unsubscribe send an email to checkin-list-le...@monetdb.org