From a9cf21ef2e4770825d693e29f6b2d02d4291230e Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 7 Jul 2022 12:41:15 +0100 Subject: [PATCH] Use ARRAY_SIZE macro in various places in nbdkit (cherry picked from commit 0a077b95b23c1d9e2ffbad5e58fc8ae46a4e547e) --- common/bitmap/test-bitmap.c | 3 ++- common/include/test-random.c | 5 ++--- common/utils/Makefile.am | 1 + common/utils/test-quotes.c | 3 ++- plugins/ocaml/Makefile.am | 1 + plugins/ocaml/plugin.c | 14 ++++++++------ plugins/ruby/Makefile.am | 1 + plugins/ruby/ruby.c | 5 +++-- plugins/ssh/ssh.c | 4 ++-- plugins/torrent/torrent.cpp | 5 ++--- server/fuzzer.c | 4 +++- server/public.c | 3 ++- server/test-public.c | 4 +++- 13 files changed, 32 insertions(+), 21 deletions(-) diff --git a/common/bitmap/test-bitmap.c b/common/bitmap/test-bitmap.c index 69e4d9ff..657cc9aa 100644 --- a/common/bitmap/test-bitmap.c +++ b/common/bitmap/test-bitmap.c @@ -45,6 +45,7 @@ #include +#include "array-size.h" #include "bitmap.h" static void @@ -71,7 +72,7 @@ test (int bpb, int blksize) exit (EXIT_FAILURE); /* Set some bits at known block numbers. */ - for (j = 0; j < sizeof blks / sizeof blks[0]; ++j) { + for (j = 0; j < ARRAY_SIZE (blks); ++j) { v = (j & 1) == 0 ? 1 : (1< #include +#include "array-size.h" #include "random.h" /* This works by comparing the result to some known test vectors. It @@ -160,8 +161,6 @@ UINT64_C(0xc6ca62fc772728b0), } }, }; -#define nr_tests (sizeof tests / sizeof tests[0]) - int main (void) { @@ -169,7 +168,7 @@ main (void) uint64_t r; unsigned errors = 0; - for (i = 0; i < nr_tests; ++i) { + for (i = 0; i < ARRAY_SIZE (tests); ++i) { struct random_state state; printf ("seed: %" PRIu64 "\n", tests[i].seed); diff --git a/common/utils/Makefile.am b/common/utils/Makefile.am index f8db853f..2c789015 100644 --- a/common/utils/Makefile.am +++ b/common/utils/Makefile.am @@ -105,6 +105,7 @@ test_quotes_SOURCES = test-quotes.c quote.c utils.h test_quotes_CPPFLAGS = \ -I$(srcdir) \ -I$(top_srcdir)/common/replacements \ + -I$(top_srcdir)/common/include \ $(NULL) test_quotes_CFLAGS = $(WARNINGS_CFLAGS) test_quotes_LDADD = \ diff --git a/common/utils/test-quotes.c b/common/utils/test-quotes.c index bb825788..4960c7f8 100644 --- a/common/utils/test-quotes.c +++ b/common/utils/test-quotes.c @@ -41,6 +41,7 @@ #include #include +#include "array-size.h" #include "utils.h" #ifdef HAVE_OPEN_MEMSTREAM @@ -93,7 +94,7 @@ main (void) size_t i; bool fail = false; - for (i = 0; i < sizeof tests / sizeof tests[0]; i++) { + for (i = 0; i < ARRAY_SIZE (tests); i++) { fail |= test (tests[i].orig, "shell_quote", shell_quote, tests[i].shell); fail |= test (tests[i].orig, "uri_quote", uri_quote, tests[i].uri); } diff --git a/plugins/ocaml/Makefile.am b/plugins/ocaml/Makefile.am index 0ee07f60..05624176 100644 --- a/plugins/ocaml/Makefile.am +++ b/plugins/ocaml/Makefile.am @@ -69,6 +69,7 @@ libnbdkitocaml_la_SOURCES = \ libnbdkitocaml_la_CPPFLAGS = \ -I$(OCAMLLIB) \ -I$(top_srcdir)/include \ + -I$(top_srcdir)/common/include \ -DCAML_NAME_SPACE \ $(NULL) diff --git a/plugins/ocaml/plugin.c b/plugins/ocaml/plugin.c index 8dde45a0..2fe4e460 100644 --- a/plugins/ocaml/plugin.c +++ b/plugins/ocaml/plugin.c @@ -49,6 +49,8 @@ #define NBDKIT_API_VERSION 2 #include +#include "array-size.h" + #include "plugin.h" /* This constructor runs when the plugin loads, and initializes the @@ -637,7 +639,7 @@ pread_wrapper (void *h, void *buf, uint32_t count, uint64_t offset, flagsv = Val_flags (flags); value args[] = { *(value *) h, countv, offsetv, flagsv }; - rv = caml_callbackN_exn (pread_fn, sizeof args / sizeof args[0], args); + rv = caml_callbackN_exn (pread_fn, ARRAY_SIZE (args), args); if (Is_exception_result (rv)) { nbdkit_error ("%s", caml_format_exception (Extract_exception (rv))); CAMLreturnT (int, -1); @@ -666,7 +668,7 @@ pwrite_wrapper (void *h, const void *buf, uint32_t count, uint64_t offset, flagsv = Val_flags (flags); value args[] = { *(value *) h, strv, offsetv, flagsv }; - rv = caml_callbackN_exn (pwrite_fn, sizeof args / sizeof args[0], args); + rv = caml_callbackN_exn (pwrite_fn, ARRAY_SIZE (args), args); if (Is_exception_result (rv)) { nbdkit_error ("%s", caml_format_exception (Extract_exception (rv))); CAMLreturnT (int, -1); @@ -705,7 +707,7 @@ trim_wrapper (void *h, uint32_t count, uint64_t offset, uint32_t flags) flagsv = Val_flags (flags); value args[] = { *(value *) h, countv, offsetv, flagsv }; - rv = caml_callbackN_exn (trim_fn, sizeof args / sizeof args[0], args); + rv = caml_callbackN_exn (trim_fn, ARRAY_SIZE (args), args); if (Is_exception_result (rv)) { nbdkit_error ("%s", caml_format_exception (Extract_exception (rv))); CAMLreturnT (int, -1); @@ -726,7 +728,7 @@ zero_wrapper (void *h, uint32_t count, uint64_t offset, uint32_t flags) flagsv = Val_flags (flags); value args[] = { *(value *) h, countv, offsetv, flagsv }; - rv = caml_callbackN_exn (zero_fn, sizeof args / sizeof args[0], args); + rv = caml_callbackN_exn (zero_fn, ARRAY_SIZE (args), args); if (Is_exception_result (rv)) { nbdkit_error ("%s", caml_format_exception (Extract_exception (rv))); CAMLreturnT (int, -1); @@ -748,7 +750,7 @@ extents_wrapper (void *h, uint32_t count, uint64_t offset, uint32_t flags, flagsv = Val_flags (flags); value args[] = { *(value *) h, countv, offsetv, flagsv }; - rv = caml_callbackN_exn (extents_fn, sizeof args / sizeof args[0], args); + rv = caml_callbackN_exn (extents_fn, ARRAY_SIZE (args), args); if (Is_exception_result (rv)) { nbdkit_error ("%s", caml_format_exception (Extract_exception (rv))); CAMLreturnT (int, -1); @@ -788,7 +790,7 @@ cache_wrapper (void *h, uint32_t count, uint64_t offset, uint32_t flags) flagsv = Val_flags (flags); value args[] = { *(value *) h, countv, offsetv, flagsv }; - rv = caml_callbackN_exn (cache_fn, sizeof args / sizeof args[0], args); + rv = caml_callbackN_exn (cache_fn, ARRAY_SIZE (args), args); if (Is_exception_result (rv)) { nbdkit_error ("%s", caml_format_exception (Extract_exception (rv))); CAMLreturnT (int, -1); diff --git a/plugins/ruby/Makefile.am b/plugins/ruby/Makefile.am index 456d03b1..2453ba05 100644 --- a/plugins/ruby/Makefile.am +++ b/plugins/ruby/Makefile.am @@ -47,6 +47,7 @@ nbdkit_ruby_plugin_la_SOURCES = \ nbdkit_ruby_plugin_la_CPPFLAGS = \ -I$(top_srcdir)/include \ + -I$(top_srcdir)/common/include \ $(NULL) nbdkit_ruby_plugin_la_CFLAGS = \ $(WARNINGS_CFLAGS) \ diff --git a/plugins/ruby/ruby.c b/plugins/ruby/ruby.c index a038d8e8..6aaf03e4 100644 --- a/plugins/ruby/ruby.c +++ b/plugins/ruby/ruby.c @@ -44,6 +44,8 @@ #include #endif +#include "array-size.h" + static VALUE nbdkit_module = Qnil; static int last_error; @@ -206,8 +208,7 @@ plugin_rb_config (const char *key, const char *value) /* Load the Ruby script into the interpreter. */ const char *options[] = { "--", script }; - code = ruby_options (sizeof options / sizeof options[0], - (char **) options); + code = ruby_options (ARRAY_SIZE (options), (char **) options); /* Check if we managed to compile the Ruby script to code. */ if (!ruby_executable_node (code, &state)) { diff --git a/plugins/ssh/ssh.c b/plugins/ssh/ssh.c index 77cfcf6c..39d77e44 100644 --- a/plugins/ssh/ssh.c +++ b/plugins/ssh/ssh.c @@ -50,6 +50,7 @@ #include +#include "array-size.h" #include "const-string-vector.h" #include "minmax.h" @@ -82,10 +83,9 @@ log_callback (int priority, const char *function, const char *message, void *vp) { const char *levels[] = { "none", "warning", "protocol", "packet", "function" }; - const size_t nr_levels = sizeof levels / sizeof levels[0]; const char *level; - if (priority >= 0 && priority < nr_levels) + if (priority >= 0 && priority < ARRAY_SIZE (levels)) level = levels[priority]; else level = "unknown"; diff --git a/plugins/torrent/torrent.cpp b/plugins/torrent/torrent.cpp index d8cbda95..0dcf0d10 100644 --- a/plugins/torrent/torrent.cpp +++ b/plugins/torrent/torrent.cpp @@ -52,6 +52,7 @@ #include #include +#include "array-size.h" extern "C" { #include "cleanup.h" }; @@ -101,8 +102,6 @@ static struct setting settings[] = { { "user-agent", "user_agent", pack.user_agent, STRING }, }; -static const size_t nr_settings = sizeof settings / sizeof settings[0]; - static libtorrent::alert_category_t alerts = libtorrent::alert_category::error | libtorrent::alert_category::piece_progress @@ -221,7 +220,7 @@ torrent_config (const char *key, const char *value) /* Settings. */ else { - for (size_t i = 0; i < nr_settings; ++i) { + for (size_t i = 0; i < ARRAY_SIZE (settings); ++i) { if (strcmp (key, settings[i].name) == 0 || (settings[i].altname && strcmp (key, settings[i].altname) == 0)) { switch (settings[i].type) { diff --git a/server/fuzzer.c b/server/fuzzer.c index 4bbb0061..6c57e0f6 100644 --- a/server/fuzzer.c +++ b/server/fuzzer.c @@ -43,6 +43,8 @@ #include #include +#include "array-size.h" + #include "internal.h" #ifndef ENABLE_LIBFUZZER @@ -122,7 +124,7 @@ server (int sock) "plugins/memory/.libs/nbdkit-memory-plugin." SOEXT, "1M", NULL }; - const int argc = sizeof argv / sizeof argv[0] - 1; + const int argc = ARRAY_SIZE (argv) - 1; int saved_stdin, saved_stdout; /* Make the socket appear as stdin and stdout of the process, saving diff --git a/server/public.c b/server/public.c index d9ed0d9c..a93041fe 100644 --- a/server/public.c +++ b/server/public.c @@ -70,6 +70,7 @@ #include #endif +#include "array-size.h" #include "ascii-ctype.h" #include "ascii-string.h" #include "get_current_dir_name.h" @@ -712,7 +713,7 @@ nbdkit_nanosleep (unsigned sec, unsigned nsec) */ if (sigfillset(&all)) abort (); - switch (ppoll (fds, sizeof fds / sizeof fds[0], &ts, &all)) { + switch (ppoll (fds, ARRAY_SIZE (fds), &ts, &all)) { case -1: assert (errno != EINTR); nbdkit_error ("poll: %m"); diff --git a/server/test-public.c b/server/test-public.c index 2e6a7d8f..1cb759d1 100644 --- a/server/test-public.c +++ b/server/test-public.c @@ -43,6 +43,8 @@ #include "internal.h" +#include "array-size.h" + static bool error_flagged; /* Stubs for linking against minimal source files, and for proving @@ -167,7 +169,7 @@ test_nbdkit_parse_size (void) { "1E", 1024LL * 1024 * 1024 * 1024 * 1024 * 1024 }, }; - for (i = 0; i < sizeof pairs / sizeof pairs[0]; i++) { + for (i = 0; i < ARRAY_SIZE (pairs); i++) { int64_t r; error_flagged = false; -- 2.31.1