From 784e498c4a9c1f03266ced5ec3f55c3a6c94b80d Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Fri, 21 Oct 2022 14:47:44 +0200 Subject: refactor: clang-tidy fixes to silence clangd warning (#20683) * refactor: readability-uppercase-literal-suffix * refactor: readability-named-parameter * refactor: bugprone-suspicious-string-compare * refactor: google-readability-casting * refactor: readability-redundant-control-flow * refactor: bugprone-too-small-loop-variable * refactor: readability-non-const-parameter * refactor: readability-avoid-const-params-in-decls * refactor: google-readability-todo * refactor: readability-inconsistent-declaration-parameter-name * refactor: bugprone-suspicious-missing-comma * refactor: remove noisy or slow warnings --- src/nvim/os/time.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/os/time.c') diff --git a/src/nvim/os/time.c b/src/nvim/os/time.c index 396bf6986a..161c8d28b8 100644 --- a/src/nvim/os/time.c +++ b/src/nvim/os/time.c @@ -67,7 +67,7 @@ void os_delay(uint64_t ms, bool ignoreinput) } LOOP_PROCESS_EVENTS_UNTIL(&main_loop, NULL, (int)ms, got_int); } else { - os_microdelay(ms * 1000u, ignoreinput); + os_microdelay(ms * 1000U, ignoreinput); } } @@ -80,10 +80,10 @@ void os_delay(uint64_t ms, bool ignoreinput) /// If false, waiting is aborted on any input. void os_microdelay(uint64_t us, bool ignoreinput) { - uint64_t elapsed = 0u; + uint64_t elapsed = 0U; uint64_t base = uv_hrtime(); // Convert microseconds to nanoseconds, or UINT64_MAX on overflow. - const uint64_t ns = (us < UINT64_MAX / 1000u) ? us * 1000u : UINT64_MAX; + const uint64_t ns = (us < UINT64_MAX / 1000U) ? us * 1000U : UINT64_MAX; uv_mutex_lock(&delay_mutex); @@ -92,7 +92,7 @@ void os_microdelay(uint64_t us, bool ignoreinput) // Else we check for input in ~100ms intervals. const uint64_t ns_delta = ignoreinput ? ns - elapsed - : MIN(ns - elapsed, 100000000u); // 100ms + : MIN(ns - elapsed, 100000000U); // 100ms const int rv = uv_cond_timedwait(&delay_cond, &delay_mutex, ns_delta); if (0 != rv && UV_ETIMEDOUT != rv) { -- cgit From 66360675cf4d091b7460e4a8e1435c13216c1929 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Sun, 11 Sep 2022 17:12:44 +0200 Subject: build: allow IWYU to fix includes for all .c files Allow Include What You Use to remove unnecessary includes and only include what is necessary. This helps with reducing compilation times and makes it easier to visualise which dependencies are actually required. Work on https://github.com/neovim/neovim/issues/549, but doesn't close it since this only works fully for .c files and not headers. --- src/nvim/os/time.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/nvim/os/time.c') diff --git a/src/nvim/os/time.c b/src/nvim/os/time.c index 161c8d28b8..7ba2bd155e 100644 --- a/src/nvim/os/time.c +++ b/src/nvim/os/time.c @@ -1,22 +1,34 @@ // This is an open source non-commercial project. Dear PVS-Studio, please check // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com -#include +#include #include +#include +#include +#include +#include + #include -#include "nvim/assert.h" +#include "auto/config.h" #include "nvim/event/loop.h" +#include "nvim/gettext.h" +#include "nvim/globals.h" +#include "nvim/log.h" +#include "nvim/macros.h" #include "nvim/main.h" +#include "nvim/memory.h" #include "nvim/os/input.h" #include "nvim/os/os.h" #include "nvim/os/time.h" +struct tm; + static uv_mutex_t delay_mutex; static uv_cond_t delay_cond; #ifdef INCLUDE_GENERATED_DECLARATIONS -# include "os/time.c.generated.h" +# include "os/time.c.generated.h" // IWYU pragma: export #endif /// Initializes the time module -- cgit From c2a9c64d231ff234a32189996ed88a8c91c0c046 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 25 Nov 2022 11:34:59 +0800 Subject: vim-patch:8.2.4155: translating strftime() argument results in check error Problem: Translating strftime() argument results in check error. Solution: Add gettext comment. https://github.com/vim/vim/commit/7e93577a957e4f402bb690c4c8629fd831e24a9d Co-authored-by: Bram Moolenaar --- src/nvim/os/time.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/nvim/os/time.c') diff --git a/src/nvim/os/time.c b/src/nvim/os/time.c index 7ba2bd155e..7fc43d7991 100644 --- a/src/nvim/os/time.c +++ b/src/nvim/os/time.c @@ -188,6 +188,7 @@ char *os_ctime_r(const time_t *restrict clock, char *restrict result, size_t res if (clock_local_ptr == NULL) { xstrlcpy(result, _("(Invalid)"), result_len); } else { + // xgettext:no-c-format strftime(result, result_len, _("%a %b %d %H:%M:%S %Y"), clock_local_ptr); } xstrlcat(result, "\n", result_len); -- cgit From 7328c4de54ac96b39853b3f43736aff863fd209d Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 29 Nov 2022 11:18:15 +0800 Subject: vim-patch:9.0.0733: use of strftime() is not safe (#21228) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: Use of strftime() is not safe. Solution: Check the return value of strftime(). Use a larger buffer and correctly pass the available space. (Dominique Pellé, closes vim/vim#11348) https://github.com/vim/vim/commit/84d14ccdb50dc9f362066a2c83bfaf331314e5ea Co-authored-by: Dominique Pelle --- src/nvim/os/time.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/nvim/os/time.c') diff --git a/src/nvim/os/time.c b/src/nvim/os/time.c index 7fc43d7991..360565fbc5 100644 --- a/src/nvim/os/time.c +++ b/src/nvim/os/time.c @@ -186,10 +186,16 @@ char *os_ctime_r(const time_t *restrict clock, char *restrict result, size_t res struct tm *clock_local_ptr = os_localtime_r(clock, &clock_local); // MSVC returns NULL for an invalid value of seconds. if (clock_local_ptr == NULL) { - xstrlcpy(result, _("(Invalid)"), result_len); + xstrlcpy(result, _("(Invalid)"), result_len - 1); } else { // xgettext:no-c-format - strftime(result, result_len, _("%a %b %d %H:%M:%S %Y"), clock_local_ptr); + if (strftime(result, result_len - 1, _("%a %b %d %H:%M:%S %Y"), clock_local_ptr) == 0) { + // Quoting "man strftime": + // > If the length of the result string (including the terminating + // > null byte) would exceed max bytes, then strftime() returns 0, + // > and the contents of the array are undefined. + xstrlcpy(result, _("(Invalid)"), result_len - 1); + } } xstrlcat(result, "\n", result_len); return result; -- cgit From 98695b49992daa2b40eb3d5b5e4a86e99c92ed0e Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 29 Nov 2022 11:48:06 +0800 Subject: vim-patch:8.1.1313: warnings for using localtime() and ctime() (#21229) Problem: Warnings for using localtime() and ctime(). Solution: Use localtime_r() if available. Avoid using ctime(). https://github.com/vim/vim/commit/63d2555c9cefbbeeca3ec87fdd5d241e9488f9dd Co-authored-by: Bram Moolenaar --- src/nvim/os/time.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/nvim/os/time.c') diff --git a/src/nvim/os/time.c b/src/nvim/os/time.c index 360565fbc5..873302a27d 100644 --- a/src/nvim/os/time.c +++ b/src/nvim/os/time.c @@ -179,7 +179,8 @@ struct tm *os_localtime(struct tm *result) FUNC_ATTR_NONNULL_ALL /// @param result[out] Pointer to a 'char' where the result should be placed /// @param result_len length of result buffer /// @return human-readable string of current local time -char *os_ctime_r(const time_t *restrict clock, char *restrict result, size_t result_len) +char *os_ctime_r(const time_t *restrict clock, char *restrict result, size_t result_len, + bool add_newline) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET { struct tm clock_local; @@ -197,7 +198,9 @@ char *os_ctime_r(const time_t *restrict clock, char *restrict result, size_t res xstrlcpy(result, _("(Invalid)"), result_len - 1); } } - xstrlcat(result, "\n", result_len); + if (add_newline) { + xstrlcat(result, "\n", result_len); + } return result; } @@ -206,11 +209,11 @@ char *os_ctime_r(const time_t *restrict clock, char *restrict result, size_t res /// @param result[out] Pointer to a 'char' where the result should be placed /// @param result_len length of result buffer /// @return human-readable string of current local time -char *os_ctime(char *result, size_t result_len) +char *os_ctime(char *result, size_t result_len, bool add_newline) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET { time_t rawtime = time(NULL); - return os_ctime_r(&rawtime, result, result_len); + return os_ctime_r(&rawtime, result, result_len, add_newline); } /// Portable version of POSIX strptime() -- cgit