29882e
From 61d59a061acf2eeeb328864d1aef8b10b6b6a1fb Mon Sep 17 00:00:00 2001
29882e
From: rpm-build <rpm-build>
29882e
Date: Mon, 20 Feb 2017 15:06:45 +0100
29882e
Subject: [PATCH 3/3] Replace unsupported autoreallocating buffer by custom
29882e
 print_buffer
29882e
29882e
---
29882e
 src/settings.c | 106 +++++++++++++++++++++++++++++++++++++++++----------------
29882e
 1 file changed, 77 insertions(+), 29 deletions(-)
29882e
29882e
diff --git a/src/settings.c b/src/settings.c
29882e
index 8f3ae34..7d41b66 100644
29882e
--- a/src/settings.c
29882e
+++ b/src/settings.c
29882e
@@ -553,19 +553,69 @@ settings_set_free(settings_set_t **set) {
29882e
 	*set = NULL;
29882e
 }
29882e
 
29882e
+static const unsigned int BUFFER_INCR = 2048;
29882e
+/** Structure for unlimited buffer.
29882e
+ *
29882e
+ * Replace for isc_buffer with autoreallocation enabled from newer bind. */
29882e
+typedef struct print_buffer
29882e
+{
29882e
+	isc_mem_t *mctx;
29882e
+	isc_buffer_t *buffer;
29882e
+	isc_result_t allocated;
29882e
+} print_buffer_t;
29882e
+
29882e
+/** Initialize print buffer with preallocated isc_buffer. */
29882e
+static isc_result_t
29882e
+print_buffer_init(isc_mem_t *mctx, print_buffer_t *buffer)
29882e
+{
29882e
+	buffer->mctx = mctx;
29882e
+	buffer->buffer = NULL;
29882e
+	buffer->allocated =
29882e
+		isc_buffer_allocate(mctx, &buffer->buffer, BUFFER_INCR);
29882e
+	return buffer->allocated;
29882e
+}
29882e
+
29882e
+static isc_result_t
29882e
+print_buffer_increment(print_buffer_t *pb, unsigned int inclen)
29882e
+{
29882e
+	unsigned int increment = BUFFER_INCR;
29882e
+	isc_buffer_t *newbuffer = NULL;
29882e
+	unsigned int newsize;
29882e
+	if (increment < (inclen))
29882e
+		increment = (((inclen)/BUFFER_INCR)+1)*BUFFER_INCR;
29882e
+
29882e
+	newsize = isc_buffer_length(pb->buffer)+increment;
29882e
+	REQUIRE(newsize > isc_buffer_length(pb->buffer));
29882e
+	pb->allocated = isc_buffer_allocate(pb->mctx, &newbuffer, newsize);
29882e
+	if (pb->allocated == ISC_R_SUCCESS)
29882e
+	{
29882e
+		isc_buffer_putmem(newbuffer,
29882e
+			isc_buffer_base(pb->buffer),
29882e
+			isc_buffer_length(pb->buffer));
29882e
+		isc_buffer_free(&pb->buffer);
29882e
+		pb->buffer = newbuffer;
29882e
+	}
29882e
+	return pb->allocated;
29882e
+}
29882e
+
29882e
 /**
29882e
- * Append textlen bytes from text to isc_buffer pointed to by closure.
29882e
+ * Append textlen bytes from text to print_buffer pointed to by closure.
29882e
  *
29882e
- * @pre closure is an initialized isc_buffer with autoreallocation enabled.
29882e
+ * @pre closure is an initialized print_buffer .
29882e
  */
29882e
 static void
29882e
 cfg_printer(void *closure, const char *text, int textlen) {
29882e
-	isc_buffer_t *logbuffer = closure;
29882e
-
29882e
-	REQUIRE(logbuffer != NULL);
29882e
-	REQUIRE(logbuffer->autore == ISC_TRUE);
29882e
+	struct print_buffer * pb = closure;
29882e
+	REQUIRE(pb != NULL);
29882e
+	REQUIRE(pb->buffer != NULL);
29882e
+	REQUIRE(pb->mctx != NULL);
29882e
+
29882e
+	/* I will append terminating '\0', make sure space is reserved */
29882e
+	if (isc_buffer_availablelength(pb->buffer) < (unsigned)(textlen+1)) {
29882e
+		print_buffer_increment(pb, textlen+1);
29882e
+	}
29882e
 
29882e
-	isc_buffer_putmem(logbuffer, (const unsigned char *)text, textlen);
29882e
+	isc_buffer_putmem(pb->buffer, (const unsigned char *)text, textlen);
29882e
 }
29882e
 
29882e
 /**
29882e
@@ -583,14 +633,12 @@ settings_set_fill(const cfg_obj_t *config, settings_set_t *set)
29882e
 {
29882e
 	isc_result_t result;
29882e
 	setting_t *setting;
29882e
-	isc_buffer_t *buf_value = NULL;
29882e
+	print_buffer_t buf_value;
29882e
 	const cfg_obj_t *cfg_value;
29882e
 	const char *str_value;
29882e
 
29882e
 	REQUIRE(cfg_obj_ismap(config) == ISC_TRUE);
29882e
-
29882e
-	CHECK(isc_buffer_allocate(set->mctx, &buf_value, ISC_BUFFER_INCR));
29882e
-	isc_buffer_setautorealloc(buf_value, ISC_TRUE);
29882e
+	CHECK(print_buffer_init(set->mctx, &buf_value));
29882e
 
29882e
 	for (setting = set->first_setting;
29882e
 	     setting->name != NULL;
29882e
@@ -605,21 +653,22 @@ settings_set_fill(const cfg_obj_t *config, settings_set_t *set)
29882e
 			/* this avoids additional quotes around the string */
29882e
 			str_value = cfg_obj_asstring(cfg_value);
29882e
 		} else {
29882e
-			cfg_print(cfg_value, cfg_printer, buf_value);
29882e
-			isc_buffer_putmem(buf_value, (unsigned char *)"\0", 1);
29882e
-			str_value = isc_buffer_base(buf_value);
29882e
+			cfg_print(cfg_value, cfg_printer, &buf_value);
29882e
+			CHECK(buf_value.allocated);
29882e
+			isc_buffer_putmem(buf_value.buffer, (unsigned char *)"\0", 1);
29882e
+			str_value = isc_buffer_base(buf_value.buffer);
29882e
 		}
29882e
 		result = set_value(set->mctx, set, setting, str_value);
29882e
 		if (result != ISC_R_SUCCESS && result != ISC_R_IGNORE)
29882e
 			goto cleanup;
29882e
-		isc_buffer_clear(buf_value);
29882e
+		isc_buffer_clear(buf_value.buffer);
29882e
 	}
29882e
 
29882e
 cleanup:
29882e
 	if (result != ISC_R_SUCCESS)
29882e
 		log_error_r("cannot parse settings for '%s'", set->name);
29882e
-	if (buf_value != NULL)
29882e
-		isc_buffer_free(&buf_value);
29882e
+	if (buf_value.buffer != NULL)
29882e
+		isc_buffer_free(&buf_value.buffer);
29882e
 	return result;
29882e
 }
29882e
 
29882e
@@ -673,46 +722,45 @@ setting_set_parse_conf(isc_mem_t *mctx, const char *name,
29882e
 	isc_result_t result;
29882e
 	cfg_obj_t *config = NULL;
29882e
 	isc_buffer_t in_buf;
29882e
-	isc_buffer_t *log_buf = NULL;
29882e
+	print_buffer_t pb;
29882e
 	cfg_parser_t *parser = NULL;
29882e
 	unsigned int len;
29882e
 
29882e
 	REQUIRE(parameters != NULL);
29882e
 
29882e
-	CHECK(isc_buffer_allocate(mctx, &log_buf, ISC_BUFFER_INCR));
29882e
-	isc_buffer_setautorealloc(log_buf, ISC_TRUE);
29882e
+	CHECK(print_buffer_init(mctx, &pb));
29882e
 
29882e
 	len = strlen(parameters);
29882e
 	isc_buffer_constinit(&in_buf, parameters, len);
29882e
 	isc_buffer_add(&in_buf, len);
29882e
 
29882e
 	CHECK(cfg_parser_create(mctx, dns_lctx, &parser));
29882e
-	result = cfg_parse_buffer2(parser, &in_buf, name, cfg_type_conf,
29882e
+	result = cfg_parse_buffer(parser, &in_buf, cfg_type_conf,
29882e
 				   &config);
29882e
 	if (result == ISC_R_SUCCESS) {
29882e
-		cfg_print(config, cfg_printer, log_buf);
29882e
+		cfg_print(config, cfg_printer, &pb;;
29882e
 		cfg_obj_log(config, dns_lctx, ISC_LOG_DEBUG(10),
29882e
 			    "configuration for dyndb instance '%s' "
29882e
 			    "(starting in file %s on line %lu):\n"
29882e
 			    "%.*s",
29882e
-			    name, file, line, isc_buffer_usedlength(log_buf),
29882e
-			    (char *)isc_buffer_base(log_buf));
29882e
+			    name, file, line, isc_buffer_usedlength(pb.buffer),
29882e
+			    (char *)isc_buffer_base(pb.buffer));
29882e
 	} else {
29882e
 		log_error("configuration for dyndb instance '%s' "
29882e
 			  "(starting in file %s on line %lu) is invalid",
29882e
 			  name, file, line);
29882e
-		cfg_print_grammar(cfg_type_conf, cfg_printer, log_buf);
29882e
+		cfg_print_grammar(cfg_type_conf, cfg_printer, &pb;;
29882e
 		log_info("expected grammar:\n"
29882e
-			 "%.*s", isc_buffer_usedlength(log_buf),
29882e
-			 (char *)isc_buffer_base(log_buf));
29882e
+			 "%.*s", isc_buffer_usedlength(pb.buffer),
29882e
+			 (char *)isc_buffer_base(pb.buffer));
29882e
 		goto cleanup;
29882e
 	}
29882e
 
29882e
 	CHECK(settings_set_fill(config, settings));
29882e
 
29882e
 cleanup:
29882e
-	if (log_buf != NULL)
29882e
-		isc_buffer_free(&log_buf);
29882e
+	if (pb.buffer != NULL)
29882e
+		isc_buffer_free(&pb.buffer);
29882e
 	if (config != NULL)
29882e
 		cfg_obj_destroy(parser, &config);
29882e
 	if (parser != NULL)
29882e
-- 
29882e
2.9.3
29882e