Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Z2 #862

Merged
merged 2 commits into from
Mar 11, 2024
Merged

Z2 #862

merged 2 commits into from
Mar 11, 2024

Conversation

alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Dec 3, 2023

Queued after #861 (done)

@alejandro-colomar
Copy link
Collaborator Author

v1b changes:

  • Rebase to master
$ git range-diff master..gh/z2 shadow/master..z2 
1:  feb5293e = 1:  535e3f65 lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP()

Copy link
Collaborator

@ikerexxe ikerexxe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the comments before the macro definition? This would make them more intuitive to read.

@ikerexxe
Copy link
Collaborator

Update the documentation, and improve the example, which was rather confusing.

Perhaps adding unit tests would help you explain better how to use it.

@alejandro-colomar
Copy link
Collaborator Author

v1c changes:

  • Rebase to master
$ git range-diff gh/z2^..gh/z2 shadow/master..z2 
1:  535e3f65 = 1:  2c82fa27 lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP()

@alejandro-colomar
Copy link
Collaborator Author

v2 changes:

  • Add tests
  • Put comment above definition
  • Update copyright
  • Update include guard
  • Don't remove the .c file
$ git range-diff shadow/master gh/z2 z2 
1:  2c82fa27 ! 1:  0804e89c lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP()
    @@ Commit message
         lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP()
     
         The function should never be used; it's always used via its wrapper
    -    macro.  To simplify, and reduce chances of confusion, remove the
    +    macro.  To simplify, and reduce chances of confusion: remove the
         function, and implement the macro directly in terms of
         stpcpy(mempcpy(strnlen())).
     
    @@ Commit message
         confusing.
     
         Cc: "Serge E. Hallyn" <[email protected]>
    +    Cc: Iker Pedrosa <[email protected]>
         Signed-off-by: Alejandro Colomar <[email protected]>
     
      ## lib/Makefile.am ##
    @@ lib/Makefile.am: libshadow_la_SOURCES = \
        strtoday.c \
        sub.c \
     
    - ## lib/string/zustr2stp.c (deleted) ##
    + ## lib/string/zustr2stp.c ##
     @@
     -/*
     - * SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar <[email protected]>
     - * SPDX-License-Identifier: BSD-3-Clause
     - */
    --
    --
    --#include <config.h>
    --
    ++// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <[email protected]>
    ++// SPDX-License-Identifier: BSD-3-Clause
    + 
    + 
    + #include <config.h>
    + 
     -#include <stddef.h>
     -
     -#ident "$Id$"
     -
    --#include "string/zustr2stp.h"
    + #include "string/zustr2stp.h"
     -
     -
     -extern inline char *zustr2stp(char *restrict dst, const char *restrict src,
    @@ lib/string/zustr2stp.c (deleted)
     
      ## lib/string/zustr2stp.h ##
     @@
    +-/*
    +- * SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar <[email protected]>
    +- * SPDX-License-Identifier: BSD-3-Clause
    +- */
    ++// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <[email protected]>
    ++// SPDX-License-Identifier: BSD-3-Clause
    + 
    + 
    +-#ifndef SHADOW_INCLUDE_LIBMISC_ZUSTR2STP_H_
    +-#define SHADOW_INCLUDE_LIBMISC_ZUSTR2STP_H_
    ++#ifndef SHADOW_INCLUDE_LIB_STRING_ZUSTR2STP_H_
    ++#define SHADOW_INCLUDE_LIB_STRING_ZUSTR2STP_H_
    + 
    + 
      #include <config.h>
      
      #include <assert.h>
    @@ lib/string/zustr2stp.h
      #include <string.h>
      
      #include "must_be.h"
    -@@
    - ({                                                                            \
    -   static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \
    -                                                                               \
    + #include "sizeof.h"
    + 
    + 
    +-#define ZUSTR2STP(dst, src)                                                   \
    +-({                                                                            \
    +-  static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \
    +-                                                                              \
     -  zustr2stp(dst, src, NITEMS(src));                                     \
    -+  stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), "");             \
    - })
    - 
    - 
    +-})
    +-
    +-
     -inline char *zustr2stp(char *restrict dst, const char *restrict src, size_t sz);
     -
     -
    @@ lib/string/zustr2stp.h
       *
       * DESCRIPTION
     - *        This function copies the null-padded character sequence pointed
    -+ *        This macro copies the null-padded character sequence pointed
    -  *        to by src, into a string at the buffer pointed to by dst.
    +- *        to by src, into a string at the buffer pointed to by dst.
    ++ *        This macro copies at most NITEMS(src) non-null bytes from the
    ++ *        array pointed to by src, followed by a null character, to the
    ++ *        buffer pointed to by dst.
       *
       * RETURN VALUE
    -@@ lib/string/zustr2stp.h: inline char *zustr2stp(char *restrict dst, const char *restrict src, size_t sz);
    +  *        dst + strlen(dst)
       *                This function returns a pointer to the terminating NUL
       *                byte.
       *
    @@ lib/string/zustr2stp.h: inline char *zustr2stp(char *restrict dst, const char *r
     -{
     -  return stpcpy(mempcpy(dst, src, strnlen(src, sz)), "");
     -}
    --
    --
    ++#define ZUSTR2STP(dst, src)                                                   \
    ++({                                                                            \
    ++  static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \
    ++                                                                              \
    ++  stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), "");             \
    ++})
    + 
    + 
      #endif  // include guard
-:  -------- > 2:  32e48544 tests/unit/test_zustr2stp.c: Test ZUSTR2STP()

@alejandro-colomar
Copy link
Collaborator Author

v2b changes:

  • Fix check of return value of strcmp(3) in tests
$ git range-diff shadow/master gh/z2 z2 
1:  0804e89c = 1:  0804e89c lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP()
2:  32e48544 ! 2:  4ac1d4ab tests/unit/test_zustr2stp.c: Test ZUSTR2STP()
    @@ tests/unit/test_zustr2stp.c (new)
     +  char  dst[4];
     +
     +  ZUSTR2STP(&dst, src);
    -+  assert(strcmp("123", dst));
    ++  assert(strcmp("123", dst) == 0);
     +
     +  src[2] = '\0';
     +  ZUSTR2STP(&dst, src);
    -+  assert(strcmp("12", dst));
    ++  assert(strcmp("12", dst) == 0);
     +
     +  src[1] = '\0';
     +  ZUSTR2STP(&dst, src);
    -+  assert(strcmp("1", dst));
    ++  assert(strcmp("1", dst) == 0);
     +
     +  src[0] = '\0';
     +  ZUSTR2STP(&dst, src);
    -+  assert(strcmp("", dst));
    ++  assert(strcmp("", dst) == 0);
     +}

@alejandro-colomar
Copy link
Collaborator Author

v3 changes:

  • Test return value of ZUSTR2STP().
$ git range-diff shadow/master gh/z2 z2 
1:  0804e89c = 1:  0804e89c lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP()
2:  4ac1d4ab ! 2:  043bd9d3 tests/unit/test_zustr2stp.c: Test ZUSTR2STP()
    @@ tests/unit/test_zustr2stp.c (new)
     +  char  src[3] = {'1', '2', '3'};
     +  char  dst[4];
     +
    -+  ZUSTR2STP(&dst, src);
    ++  assert(ZUSTR2STP(&dst, src) == dst + strlen("123"));
     +  assert(strcmp("123", dst) == 0);
     +
     +  src[2] = '\0';
    -+  ZUSTR2STP(&dst, src);
    ++  assert(ZUSTR2STP(&dst, src) == dst + strlen("12"));
     +  assert(strcmp("12", dst) == 0);
     +
     +  src[1] = '\0';
    -+  ZUSTR2STP(&dst, src);
    ++  assert(ZUSTR2STP(&dst, src) == dst + strlen("1"));
     +  assert(strcmp("1", dst) == 0);
     +
     +  src[0] = '\0';
    -+  ZUSTR2STP(&dst, src);
    ++  assert(ZUSTR2STP(&dst, src) == dst + strlen(""));
     +  assert(strcmp("", dst) == 0);
     +}

@alejandro-colomar
Copy link
Collaborator Author

v3b changes:

  • Rebase to master
$ git range-diff 6fb7fe11..gh/z2 shadow/master..z2 
1:  0804e89c = 1:  f0f7fb11 lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP()
2:  043bd9d3 = 2:  13a54230 tests/unit/test_zustr2stp.c: Test ZUSTR2STP()

@alejandro-colomar alejandro-colomar marked this pull request as draft February 13, 2024 22:20
@alejandro-colomar alejandro-colomar marked this pull request as ready for review February 19, 2024 16:32
@alejandro-colomar
Copy link
Collaborator Author

v3c changes:

  • Rebase to master
$ git range-diff alx/master..gh/z2 shadow/master..z2 
1:  f0f7fb11 = 1:  b5278632 lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP()
2:  13a54230 = 2:  ce03092f tests/unit/test_zustr2stp.c: Test ZUSTR2STP()

@alejandro-colomar
Copy link
Collaborator Author

v4 changes:

  • Use cmocka's assert_true(); not assert(3). It shows better in the logs.
$ git range-diff gh/master gh/z2 z2 
1:  b5278632 = 1:  b5278632 lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP()
2:  ce03092f ! 2:  f56f0bed tests/unit/test_zustr2stp.c: Test ZUSTR2STP()
    @@ tests/unit/test_zustr2stp.c (new)
     +
     +#include "string/zustr2stp.h"
     +
    -+#undef NDEBUG
    -+#include <assert.h>
    -+
     +
     +static void test_ZUSTR2STP(void **state);
     +
    @@ tests/unit/test_zustr2stp.c (new)
     +  char  src[3] = {'1', '2', '3'};
     +  char  dst[4];
     +
    -+  assert(ZUSTR2STP(&dst, src) == dst + strlen("123"));
    -+  assert(strcmp("123", dst) == 0);
    ++  assert_true(ZUSTR2STP(&dst, src) == dst + strlen("123"));
    ++  assert_true(strcmp("123", dst) == 0);
     +
     +  src[2] = '\0';
    -+  assert(ZUSTR2STP(&dst, src) == dst + strlen("12"));
    -+  assert(strcmp("12", dst) == 0);
    ++  assert_true(ZUSTR2STP(&dst, src) == dst + strlen("12"));
    ++  assert_true(strcmp("12", dst) == 0);
     +
     +  src[1] = '\0';
    -+  assert(ZUSTR2STP(&dst, src) == dst + strlen("1"));
    -+  assert(strcmp("1", dst) == 0);
    ++  assert_true(ZUSTR2STP(&dst, src) == dst + strlen("1"));
    ++  assert_true(strcmp("1", dst) == 0);
     +
     +  src[0] = '\0';
    -+  assert(ZUSTR2STP(&dst, src) == dst + strlen(""));
    -+  assert(strcmp("", dst) == 0);
    ++  assert_true(ZUSTR2STP(&dst, src) == dst + strlen(""));
    ++  assert_true(strcmp("", dst) == 0);
     +}

The function should never be used; it's always used via its wrapper
macro.  To simplify, and reduce chances of confusion: remove the
function, and implement the macro directly in terms of
stpcpy(mempcpy(strnlen())).

Update the documentation, and improve the example, which was rather
confusing.

Cc: "Serge E. Hallyn" <[email protected]>
Cc: Iker Pedrosa <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
@alejandro-colomar
Copy link
Collaborator Author

v4b changes:

  • Rebase to master
$ git range-diff gh/master..gh/z2 alx/master..z2 
1:  b5278632 = 1:  86f8964e lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP()
2:  f56f0bed = 2:  6d0bb0b7 tests/unit/test_zustr2stp.c: Test ZUSTR2STP()

@hallyn hallyn merged commit 5ce1b0a into shadow-maint:master Mar 11, 2024
9 checks passed
@alejandro-colomar alejandro-colomar deleted the z2 branch March 11, 2024 01:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants