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

lib/: Use NITEMS() instead of SIZEOF_ARRAY() where number of elements is meant #849

Merged
merged 1 commit into from
Nov 27, 2023

Conversation

alejandro-colomar
Copy link
Collaborator

For arrays of char, both NITEMS() and SIZEOF_ARRAY() return the same value. However, NITEMS() is more appropriate. Think of wide-character equivalents of the same code; with NITEMS(), they would continue to be valid, while with SIZEOF_ARRAY(), they would be wrong.

@alejandro-colomar
Copy link
Collaborator Author

v2 changes:

  • Revert a change in the implementation of ZUSTR2STP().
$ git range-diff shadow/master gh/nitems nitems 
1:  ca51a134 ! 1:  38d1b779 lib/: Use NITEMS() instead of SIZEOF_ARRAY() where number of elements is meant
    @@ Commit message
         equivalents of the same code; with NITEMS(), they would continue to be
         valid, while with SIZEOF_ARRAY(), they would be wrong.
     
    +    In the implementation of ZUSTR2STP(), we want SIZEOF_ARRAY() within the
    +    static assert, because we're just comparing the sizes of the source and
    +    destination buffers, and we don't care if we compare sizes of numbers of
    +    elements, and using sizes is just simpler.  But we want NITEMS() in the
    +    zustr2stp() call, where we want to copy a specific number of characters.
    +
         Signed-off-by: Alejandro Colomar <[email protected]>
     
      ## lib/strtcpy.h ##
    @@ lib/utmp.c: static
     
      ## lib/zustr2stp.h ##
     @@
    - 
    - #define ZUSTR2STP(dst, src)                                                   \
      ({                                                                            \
    --  static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \
    -+  static_assert(!is_array(dst) || sizeof(dst) > NITEMS(src), "");       \
    +   static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \
                                                                                    \
     -  zustr2stp(dst, src, SIZEOF_ARRAY(src));                               \
     +  zustr2stp(dst, src, NITEMS(src));                                     \

@alejandro-colomar alejandro-colomar force-pushed the nitems branch 2 times, most recently from 38d1b77 to 6c72182 Compare November 23, 2023 13:21
@alejandro-colomar
Copy link
Collaborator Author

v2b changes:

  • tfix
$ git range-diff shadow/master gh/nitems nitems 
1:  38d1b779 ! 1:  6c72182d lib/: Use NITEMS() instead of SIZEOF_ARRAY() where number of elements is meant
    @@ Commit message
     
         In the implementation of ZUSTR2STP(), we want SIZEOF_ARRAY() within the
         static assert, because we're just comparing the sizes of the source and
    -    destination buffers, and we don't care if we compare sizes of numbers of
    +    destination buffers, and we don't care if we compare sizes or numbers of
         elements, and using sizes is just simpler.  But we want NITEMS() in the
         zustr2stp() call, where we want to copy a specific number of characters.
     

@alejandro-colomar
Copy link
Collaborator Author

v2c changes:

  • Rebase to master
$ git range-diff master..gh/nitems shadow/master..nitems 
1:  6c72182d = 1:  f760d404 lib/: Use NITEMS() instead of SIZEOF_ARRAY() where number of elements is meant

lib/utmp.c Show resolved Hide resolved
… is meant

For arrays of char, both NITEMS() and SIZEOF_ARRAY() return the same
value.  However, NITEMS() is more appropriate.  Think of wide-character
equivalents of the same code; with NITEMS(), they would continue to be
valid, while with SIZEOF_ARRAY(), they would be wrong.

In the implementation of ZUSTR2STP(), we want SIZEOF_ARRAY() within the
static assert, because we're just comparing the sizes of the source and
destination buffers, and we don't care if we compare sizes or numbers of
elements, and using sizes is just simpler.  But we want NITEMS() in the
zustr2stp() call, where we want to copy a specific number of characters.

Signed-off-by: Alejandro Colomar <[email protected]>
@alejandro-colomar
Copy link
Collaborator Author

v2d changes:

  • Rebase to master
$ git range-diff master..gh/nitems shadow/master..nitems 
1:  f760d404 = 1:  fd051a19 lib/: Use NITEMS() instead of SIZEOF_ARRAY() where number of elements is meant

@hallyn hallyn self-requested a review November 26, 2023 21:34
lib/strtcpy.h Show resolved Hide resolved
@hallyn hallyn merged commit 721b909 into shadow-maint:master Nov 27, 2023
@alejandro-colomar alejandro-colomar deleted the nitems branch November 27, 2023 08:52
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.

2 participants