@@ -225,21 +225,20 @@
bool
buf_printf(struct buffer *buf, const char *format, ...)
{
- int ret = false;
+ bool ret = false;
if (buf_defined(buf))
{
- uint8_t *ptr = BEND(buf);
+ char *ptr = (char *)BEND(buf);
int cap = buf_forward_capacity(buf);
- if (cap > 0)
+ if (cap > 1) /* can write more than \0 */
{
int stat;
va_list arglist;
va_start(arglist, format);
- stat = vsnprintf((char *)ptr, cap, format, arglist);
+ stat = vsnprintf(ptr, cap, format, arglist);
va_end(arglist);
- *(buf->data + buf->capacity - 1) = 0; /* windows vsnprintf needs this */
- buf->len += (int)strlen((char *)ptr);
+ buf->len += (int)strlen(ptr);
if (stat >= 0 && stat < cap)
{
ret = true;
@@ -249,19 +248,16 @@
return ret;
}
-bool
+void
buf_puts(struct buffer *buf, const char *str)
{
- int ret = false;
- uint8_t *ptr = BEND(buf);
+ char *ptr = (char *)BEND(buf);
int cap = buf_forward_capacity(buf);
- if (cap > 0)
+ if (cap > 1) /* can write more than \0 */
{
- int str_len = (int)strlcpy((char *)ptr, str, cap);
+ int str_len = (int)strlcpy(ptr, str, cap);
buf->len += (str_len < cap) ? str_len : cap - 1;
- ret = true;
}
- return ret;
}
/*
@@ -791,10 +791,8 @@
*
* @param buf The buffer to append to.
* @param str The null-terminated string to append.
- *
- * @return true if the string fit in the buffer, false if it was truncated.
*/
-bool buf_puts(struct buffer *buf, const char *str);
+void buf_puts(struct buffer *buf, const char *str);
/*
@@ -62,6 +62,10 @@
buf_printf(&buf, "%d", 123);
buf_printf(&buf, "%s", "some text, too long to fit");
assert_buf_equals_str(&buf, "123some text, t");
+ assert_int_equal(BCAP(&buf), 1); /* \0 byte not part of len */
+
+ buf_printf(&buf, "%s", "more");
+ assert_buf_equals_str(&buf, "123some text, t");
buf_catrunc(&buf, "...");
assert_buf_equals_str(&buf, "123some text...");
@@ -79,6 +83,30 @@
}
static void
+test_buffer_puts(void **state)
+{
+ struct gc_arena gc = gc_new();
+ struct buffer buf = alloc_buf_gc(5, &gc);
+
+ buf_puts(&buf, "1234");
+ assert_string_equal(BSTR(&buf), "1234");
+
+ buf_reset_len(&buf);
+ buf_puts(&buf, "12345");
+ assert_string_equal(BSTR(&buf), "1234");
+
+ /* buf already full */
+ buf_puts(&buf, "12345");
+ assert_string_equal(BSTR(&buf), "1234");
+
+ struct buffer one_byte_buf = alloc_buf_gc(1, &gc);
+ buf_puts(&one_byte_buf, "x");
+ assert_string_equal(BSTR(&one_byte_buf), "");
+
+ gc_free(&gc);
+}
+
+static void
test_buffer_format_hex_ex(void **state)
{
const int input_size = 10;
@@ -632,6 +660,7 @@
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_buffer_strprefix),
cmocka_unit_test(test_buffer_printf_catrunc),
+ cmocka_unit_test(test_buffer_puts),
cmocka_unit_test(test_buffer_format_hex_ex),
cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_empty,
test_buffer_list_setup, test_buffer_list_teardown),