summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Nagy <mnagy@redhat.com>2009-01-14 09:53:10 +0100
committerMartin Nagy <mnagy@redhat.com>2009-01-14 09:53:10 +0100
commit55cd3891797573e07daba831514c73f94a5831ac (patch)
tree92700ba641e6ec1745b884c29ab862ab8764824d
parent14c6180ac9875059c38dce0240bb383d676164ca (diff)
downloadldap_driver_testing-55cd3891797573e07daba831514c73f94a5831ac.tar.gz
ldap_driver_testing-55cd3891797573e07daba831514c73f94a5831ac.tar.xz
ldap_driver_testing-55cd3891797573e07daba831514c73f94a5831ac.zip
Add two sprintf like functions to str.c, fix a bug
Bug in str__destroy() caused us to try to free a NULL pointer.
-rw-r--r--str.c66
-rw-r--r--str.h2
2 files changed, 61 insertions, 7 deletions
diff --git a/str.c b/str.c
index bf5d3c8..acad5f6 100644
--- a/str.c
+++ b/str.c
@@ -30,6 +30,9 @@
#include <dns/result.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "str.h"
@@ -41,7 +44,6 @@
#define ALLOC_BASE_SIZE 16
-
/* Custom string, these shouldn't use these directly */
struct ld_string {
char *data; /* String is stored here. */
@@ -97,6 +99,7 @@ str_alloc(ld_string_t *str, size_t len)
}
str->data = new_buffer;
+ str->allocated = new_size;
return ISC_R_SUCCESS;
}
@@ -166,15 +169,21 @@ str__destroy(ld_string_t **str _STR_MEM_FLARG)
{
IGNORE(str == NULL || *str == NULL);
+ if ((*str)->allocated) {
#if ISC_MEM_TRACKLINES
- isc__mem_put((*str)->mctx, (void *)(*str)->data,
- (*str)->allocated * sizeof(char), file, line);
- isc__mem_putanddetach(&(*str)->mctx, (void *)*str, sizeof(ld_string_t),
+ isc__mem_put((*str)->mctx, (*str)->data,
+ (*str)->allocated * sizeof(char), file, line);
+#else
+ isc_mem_put((*str)->mctx, (*str)->data,
+ (*str)->allocated * sizeof(char));
+#endif
+ }
+
+#if ISC_MEM_TRACKLINES
+ isc__mem_putanddetach(&(*str)->mctx, *str, sizeof(ld_string_t),
file, line);
#else
- isc_mem_put((*str)->mctx, (void *)(*str)->data,
- (*str)->allocated * sizeof(char));
- isc_mem_putanddetach(&(*str)->mctx, (void *)*str, sizeof(ld_string_t));
+ isc_mem_putanddetach(&(*str)->mctx, *str, sizeof(ld_string_t));
#endif
*str = NULL;
@@ -300,3 +309,46 @@ str_cat(ld_string_t *dest, const ld_string_t *src)
return str_cat_char(dest, src->data);
}
+
+/*
+ * A sprintf() like function.
+ */
+isc_result_t
+str_sprintf(ld_string_t *dest, const char *format, ...)
+{
+ isc_result_t result;
+ va_list ap;
+
+ REQUIRE(dest != NULL);
+ REQUIRE(format != NULL);
+
+ va_start(ap, format);
+ result = str_vsprintf(dest, format, ap);
+ va_end(ap);
+
+ return result;
+}
+
+isc_result_t
+str_vsprintf(ld_string_t *dest, const char *format, va_list ap)
+{
+ int len;
+ isc_result_t result;
+
+ REQUIRE(dest != NULL);
+ REQUIRE(format != NULL);
+
+ len = vsnprintf(dest->data, dest->allocated, format, ap);
+ if (len > 0) {
+ CHECK(str_alloc(dest, len));
+ len = vsnprintf(dest->data, dest->allocated, format, ap);
+ }
+
+ if (len < 0)
+ result = ISC_R_FAILURE;
+
+ return ISC_R_SUCCESS;
+
+cleanup:
+ return result;
+}
diff --git a/str.h b/str.h
index e3ff17a..c912fa8 100644
--- a/str.h
+++ b/str.h
@@ -48,6 +48,8 @@ isc_result_t str_clone(ld_string_t **dest, const ld_string_t *src _STR_MEM_FLARG
isc_result_t str_init_char(ld_string_t *dest, const char *src);
isc_result_t str_cat_char(ld_string_t *dest, const char *src);
isc_result_t str_cat(ld_string_t *dest, const ld_string_t *src);
+isc_result_t str_sprintf(ld_string_t *dest, const char *format, ...);
+isc_result_t str_vsprintf(ld_string_t *dest, const char *format, va_list ap);
/* These are pseudo-private functions and shouldn't be called directly. */
isc_result_t str__new(isc_mem_t *mctx, ld_string_t **new_str _STR_MEM_FLARG);