Skip to content

Commit efc4e7e

Browse files
authored
Merge pull request libgit2#5802 from lhchavez/git-warn-unused-result
Introduce GIT_WARN_UNUSED_RESULT
2 parents 0850b17 + 991ccdc commit efc4e7e

File tree

14 files changed

+65
-35
lines changed

14 files changed

+65
-35
lines changed

src/cc-compat.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@
4343
# define GIT_ALIGN(x,size) x
4444
#endif
4545

46-
#define GIT_UNUSED(x) ((void)(x))
46+
#if defined(__GNUC__)
47+
# define GIT_UNUSED(x) \
48+
do { \
49+
typeof(x) _unused __attribute__((unused)); \
50+
_unused = (x); \
51+
} while (0)
52+
#else
53+
# define GIT_UNUSED(x) ((void)(x))
54+
#endif
4755

4856
/* Define the printf format specifier to use for size_t output */
4957
#if defined(_MSC_VER) || defined(__MINGW32__)

src/common.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@
3030
# define __has_builtin(x) 0
3131
#endif
3232

33+
/**
34+
* Declare that a function's return value must be used.
35+
*
36+
* Used mostly to guard against potential silent bugs at runtime. This is
37+
* recommended to be added to functions that:
38+
*
39+
* - Allocate / reallocate memory. This prevents memory leaks or errors where
40+
* buffers are expected to have grown to a certain size, but could not be
41+
* resized.
42+
* - Acquire locks. When a lock cannot be acquired, that will almost certainly
43+
* cause a data race / undefined behavior.
44+
*/
45+
#if defined(__GNUC__)
46+
# define GIT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
47+
#else
48+
# define GIT_WARN_UNUSED_RESULT
49+
#endif
50+
3351
#include <assert.h>
3452
#include <errno.h>
3553
#include <limits.h>

src/odb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ int git_odb__add_default_backends(
573573
git_odb *db, const char *objects_dir,
574574
bool as_alternates, int alternate_depth)
575575
{
576-
size_t i;
576+
size_t i = 0;
577577
struct stat st;
578578
ino_t inode;
579579
git_odb_backend *loose, *packed;
@@ -582,7 +582,7 @@ int git_odb__add_default_backends(
582582
* a cross-platform workaround for this */
583583
#ifdef GIT_WIN32
584584
GIT_UNUSED(i);
585-
GIT_UNUSED(st);
585+
GIT_UNUSED(&st);
586586

587587
inode = 0;
588588
#else

src/path.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1915,7 +1915,13 @@ GIT_INLINE(bool) should_validate_longpaths(git_repository *repo)
19151915
}
19161916

19171917
#else
1918-
# define should_validate_longpaths(repo) (GIT_UNUSED(repo), false)
1918+
1919+
GIT_INLINE(bool) should_validate_longpaths(git_repository *repo)
1920+
{
1921+
GIT_UNUSED(repo);
1922+
1923+
return false;
1924+
}
19191925
#endif
19201926

19211927
int git_path_validate_workdir(git_repository *repo, const char *path)

src/refdb_fs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static int packed_reload(refdb_fs_backend *backend)
122122
*/
123123
if (error <= 0) {
124124
if (error == GIT_ENOTFOUND) {
125-
git_sortedcache_clear(backend->refcache, true);
125+
GIT_UNUSED(git_sortedcache_clear(backend->refcache, true));
126126
git_error_clear();
127127
error = 0;
128128
}
@@ -131,7 +131,7 @@ static int packed_reload(refdb_fs_backend *backend)
131131

132132
/* At this point, refresh the packed refs from the loaded buffer. */
133133

134-
git_sortedcache_clear(backend->refcache, false);
134+
GIT_UNUSED(git_sortedcache_clear(backend->refcache, false));
135135

136136
scan = (char *)packedrefs.ptr;
137137
eof = scan + packedrefs.size;
@@ -219,7 +219,7 @@ static int packed_reload(refdb_fs_backend *backend)
219219
parse_failed:
220220
git_error_set(GIT_ERROR_REFERENCE, "corrupted packed references file");
221221

222-
git_sortedcache_clear(backend->refcache, false);
222+
GIT_UNUSED(git_sortedcache_clear(backend->refcache, false));
223223
git_sortedcache_wunlock(backend->refcache);
224224
git_buf_dispose(&packedrefs);
225225

src/sortedcache.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef struct {
5858
* may be NULL. The cache makes it easy to load this and check
5959
* if it has been modified since the last load and/or write.
6060
*/
61-
int git_sortedcache_new(
61+
GIT_WARN_UNUSED_RESULT int git_sortedcache_new(
6262
git_sortedcache **out,
6363
size_t item_path_offset, /* use offsetof(struct, path-field) macro */
6464
git_sortedcache_free_item_fn free_item,
@@ -71,7 +71,7 @@ int git_sortedcache_new(
7171
* - `copy_item` can be NULL to just use memcpy
7272
* - if `lock`, grabs read lock on `src` during copy and releases after
7373
*/
74-
int git_sortedcache_copy(
74+
GIT_WARN_UNUSED_RESULT int git_sortedcache_copy(
7575
git_sortedcache **out,
7676
git_sortedcache *src,
7777
bool lock,
@@ -100,7 +100,7 @@ const char *git_sortedcache_path(git_sortedcache *sc);
100100
*/
101101

102102
/* Lock sortedcache for write */
103-
int git_sortedcache_wlock(git_sortedcache *sc);
103+
GIT_WARN_UNUSED_RESULT int git_sortedcache_wlock(git_sortedcache *sc);
104104

105105
/* Unlock sorted cache when done with write */
106106
void git_sortedcache_wunlock(git_sortedcache *sc);
@@ -120,7 +120,8 @@ void git_sortedcache_wunlock(git_sortedcache *sc);
120120
*
121121
* @return 0 if up-to-date, 1 if out-of-date, <0 on error
122122
*/
123-
int git_sortedcache_lockandload(git_sortedcache *sc, git_buf *buf);
123+
GIT_WARN_UNUSED_RESULT int git_sortedcache_lockandload(
124+
git_sortedcache *sc, git_buf *buf);
124125

125126
/* Refresh file timestamp after write completes
126127
* You should already be holding the write lock when you call this.
@@ -132,12 +133,13 @@ void git_sortedcache_updated(git_sortedcache *sc);
132133
* If `wlock` is true, grabs write lock and releases when done, otherwise
133134
* you should already be holding a write lock when you call this.
134135
*/
135-
int git_sortedcache_clear(git_sortedcache *sc, bool wlock);
136+
GIT_WARN_UNUSED_RESULT int git_sortedcache_clear(
137+
git_sortedcache *sc, bool wlock);
136138

137139
/* Find and/or insert item, returning pointer to item data.
138140
* You should already be holding the write lock when you call this.
139141
*/
140-
int git_sortedcache_upsert(
142+
GIT_WARN_UNUSED_RESULT int git_sortedcache_upsert(
141143
void **out, git_sortedcache *sc, const char *key);
142144

143145
/* Removes entry at pos from cache
@@ -155,7 +157,7 @@ int git_sortedcache_remove(git_sortedcache *sc, size_t pos);
155157
*/
156158

157159
/* Lock sortedcache for read */
158-
int git_sortedcache_rlock(git_sortedcache *sc);
160+
GIT_WARN_UNUSED_RESULT int git_sortedcache_rlock(git_sortedcache *sc);
159161

160162
/* Unlock sorted cache when done with read */
161163
void git_sortedcache_runlock(git_sortedcache *sc);

src/vector.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ typedef struct git_vector {
2626

2727
#define GIT_VECTOR_INIT {0}
2828

29-
int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp);
29+
GIT_WARN_UNUSED_RESULT int git_vector_init(
30+
git_vector *v, size_t initial_size, git_vector_cmp cmp);
3031
void git_vector_free(git_vector *v);
3132
void git_vector_free_deep(git_vector *v); /* free each entry and self */
3233
void git_vector_clear(git_vector *v);
33-
int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp);
34+
GIT_WARN_UNUSED_RESULT int git_vector_dup(
35+
git_vector *v, const git_vector *src, git_vector_cmp cmp);
3436
void git_vector_swap(git_vector *a, git_vector *b);
3537
int git_vector_size_hint(git_vector *v, size_t size_hint);
3638

src/win32/path_w32.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#define INCLUDE_win32_path_w32_h__
99

1010
#include "common.h"
11-
#include "vector.h"
1211

1312
/**
1413
* Create a Win32 path (in UCS-2 format) from a UTF-8 string. If the given

tests/core/sha1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void test_core_sha1__detect_collision_attack(void)
5252
git_oid oid, expected;
5353

5454
#ifdef GIT_SHA1_COLLISIONDETECT
55-
GIT_UNUSED(expected);
55+
GIT_UNUSED(&expected);
5656
cl_git_fail(sha1_file(&oid, FIXTURE_DIR "/shattered-1.pdf"));
5757
cl_assert_equal_s("SHA1 collision attack detected", git_error_last()->message);
5858
#else

tests/core/sortedcache.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void test_core_sortedcache__name_only(void)
5454
cl_assert_equal_i(
5555
GIT_ENOTFOUND, git_sortedcache_lookup_index(&pos, sc, "abc"));
5656

57-
git_sortedcache_clear(sc, true);
57+
cl_git_pass(git_sortedcache_clear(sc, true));
5858

5959
cl_assert_equal_sz(0, git_sortedcache_entrycount(sc));
6060
cl_assert(git_sortedcache_entry(sc, 0) == NULL);
@@ -154,7 +154,7 @@ void test_core_sortedcache__in_memory(void)
154154

155155
cl_assert_equal_i(0, free_count);
156156

157-
git_sortedcache_clear(sc, true);
157+
cl_git_pass(git_sortedcache_clear(sc, true));
158158

159159
cl_assert_equal_i(5, free_count);
160160

@@ -247,7 +247,7 @@ static void sortedcache_test_reload(git_sortedcache *sc)
247247

248248
cl_assert(git_sortedcache_lockandload(sc, &buf) > 0);
249249

250-
git_sortedcache_clear(sc, false); /* clear once we already have lock */
250+
cl_git_pass(git_sortedcache_clear(sc, false)); /* clear once we already have lock */
251251

252252
for (scan = buf.ptr; *scan; scan = after + 1) {
253253
int val = strtol(scan, &after, 0);

0 commit comments

Comments
 (0)