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: avoid clang static analyzer warning in deflate_get_offset_slot() #358

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions common_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ typedef size_t machine_word_t;
# define MAYBE_UNUSED
#endif

/* NORETURN - mark a function as never returning, e.g. due to calling abort() */
#if defined(__GNUC__) || __has_attribute(noreturn)
# define NORETURN __attribute__((noreturn))
#else
# define NORETURN
#endif

/*
* restrict - hint that writes only occur through the given pointer.
*
Expand Down
2 changes: 2 additions & 0 deletions lib/deflate_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,8 @@ deflate_get_offset_slot(u32 offset)
*/
unsigned n = (256 - offset) >> 29;

ASSERT(offset >= 1 && offset <= 32768);

return deflate_offset_slot[(offset - 1) >> n] + (n << 1);
}

Expand Down
12 changes: 10 additions & 2 deletions lib/lib_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,23 @@ int memcmp(const void *s1, const void *s2, size_t n);

#undef LIBDEFLATE_ENABLE_ASSERTIONS
#else
#include <string.h>
# include <string.h>
/*
* To prevent false positive static analyzer warnings, ensure that assertions
* are visible to the static analyzer.
*/
# ifdef __clang_analyzer__
# define LIBDEFLATE_ENABLE_ASSERTIONS
# endif
#endif

/*
* Runtime assertion support. Don't enable this in production builds; it may
* hurt performance significantly.
*/
#ifdef LIBDEFLATE_ENABLE_ASSERTIONS
void libdeflate_assertion_failed(const char *expr, const char *file, int line);
NORETURN void
libdeflate_assertion_failed(const char *expr, const char *file, int line);
#define ASSERT(expr) { if (unlikely(!(expr))) \
libdeflate_assertion_failed(#expr, __FILE__, __LINE__); }
#else
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ memcmp(const void *s1, const void *s2, size_t n)
#ifdef LIBDEFLATE_ENABLE_ASSERTIONS
#include <stdio.h>
#include <stdlib.h>
void
NORETURN void
libdeflate_assertion_failed(const char *expr, const char *file, int line)
{
fprintf(stderr, "Assertion failed: %s at %s:%d\n", expr, file, line);
Expand Down
2 changes: 1 addition & 1 deletion programs/test_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#endif

/* Abort with an error message */
_noreturn void
NORETURN void
assertion_failed(const char *expr, const char *file, int line)
{
msg("Assertion failed: %s at %s:%d", expr, file, line);
Expand Down
8 changes: 1 addition & 7 deletions programs/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@

#include <zlib.h> /* for comparison purposes */

#if defined(__GNUC__) || __has_attribute(noreturn)
# define _noreturn __attribute__((noreturn))
#else
# define _noreturn
#endif

void _noreturn
NORETURN void
assertion_failed(const char *expr, const char *file, int line);

#define ASSERT(expr) { if (unlikely(!(expr))) \
Expand Down
Loading