Skip to content

Commit 859ed5d

Browse files
committed
common: introduce GITERR_CHECK_ALLOC_BUF
We commonly have to check if a git_buf has been allocated correctly or if we ran out of memory. Introduce a new macro similar to `GITERR_CHECK_ALLOC` which checks if we ran OOM and if so returns an error. Provide a `#nodef` for Coverity to mark the error case as an abort path.
1 parent f2a554b commit 859ed5d

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

script/user_nodefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#nodef GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { __coverity_panic__(); }
9+
#nodef GITERR_CHECK_ALLOC_BUF(buf) if (buf == NULL || git_buf_oom(buf)) { __coverity_panic__(); }
910

1011
#nodef GITERR_CHECK_ALLOC_ADD(out, one, two) \
1112
if (GIT_ADD_SIZET_OVERFLOW(out, one, two)) { __coverity_panic__(); }

src/common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@
8989
*/
9090
#define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }
9191

92+
/**
93+
* Check a buffer allocation result, returning -1 if it failed.
94+
*/
95+
#define GITERR_CHECK_ALLOC_BUF(buf) if ((void *)(buf) == NULL || git_buf_oom(buf)) { return -1; }
96+
9297
/**
9398
* Check a return value and propagate result if non-zero.
9499
*/

0 commit comments

Comments
 (0)