Attention is currently required from: Arne Schwabe.

Hello Arne Schwabe,

I'd like you to do a code review.
Please visit

    https://gerrit.openvpn.net/c/openvpn/+/1997?usp=email

to review the following change.


Change subject: buffer: Fix small issues buf_printf and buf_puts
......................................................................

buffer: Fix small issues buf_printf and buf_puts

- Fix documentation for buf_puts
- Add UTs for buf_puts
Both of these changes based on a PR by
cuishuang <[email protected]>.

In both functions:
- Remove obsolete manual adding of \0
- Do not try to append if we can only overwrite
  the trailing \0 byte

Change-Id: I1d6ed5d40910359b65b9f5c5d8f56ad3d3bc827a
Signed-off-by: Frank Lichtenheld <[email protected]>
---
M src/openvpn/buffer.c
M src/openvpn/buffer.h
M tests/unit_tests/openvpn/test_buffer.c
3 files changed, 33 insertions(+), 9 deletions(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/97/1997/1

diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c
index cc1b300..d5ee9f2 100644
--- a/src/openvpn/buffer.c
+++ b/src/openvpn/buffer.c
@@ -228,18 +228,17 @@
     int 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;
@@ -253,11 +252,11 @@
 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;
     }
diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h
index c66ed1a..a4d4783 100644
--- a/src/openvpn/buffer.h
+++ b/src/openvpn/buffer.h
@@ -792,7 +792,7 @@
  * @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.
+ * @return true if the buffer was changed, i.e. at least one character was 
appended.
  */
 bool buf_puts(struct buffer *buf, const char *str);

diff --git a/tests/unit_tests/openvpn/test_buffer.c 
b/tests/unit_tests/openvpn/test_buffer.c
index bf486e0..9cdee3e 100644
--- a/tests/unit_tests/openvpn/test_buffer.c
+++ b/tests/unit_tests/openvpn/test_buffer.c
@@ -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,26 @@
 }

 static void
+test_buffer_puts(void **state)
+{
+    struct gc_arena gc = gc_new();
+    struct buffer buf = alloc_buf_gc(5, &gc);
+
+    assert_true(buf_puts(&buf, "1234"));
+    assert_string_equal(BSTR(&buf), "1234");
+
+    buf_reset_len(&buf);
+    assert_true(buf_puts(&buf, "12345"));
+    assert_string_equal(BSTR(&buf), "1234");
+
+    struct buffer one_byte_buf = alloc_buf_gc(1, &gc);
+    assert_false(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 +656,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),

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1997?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I1d6ed5d40910359b65b9f5c5d8f56ad3d3bc827a
Gerrit-Change-Number: 1997
Gerrit-PatchSet: 1
Gerrit-Owner: Frank Lichtenheld <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to