Skip to content

Commit 5a82f20

Browse files
author
Gunnar Kudrjavets
committed
Add gcc function attributes to MyRocks code with a sprinkle of assertions
Summary: Annotate MyRocks to document the current contracts using function attributes. We don't have the full power of SAL (Standard Annotation Language), but this is still better than nothing. Both Clang and GCC understand the attributes and `include/my_attribute.h` has the magic to disable them for compilers which don't support those directives. The most useful thing we get out of these changes are the annotations using `warn_unused_result` because that's the only thing which `gcc` can reasonably enforce. Using `nonnull` will help compiler to only catch the basic cases (passing `nullptr` or `NULL` literally) and rest of the attributes have the main benefit of guiding the compiler to apply more complex optimizations. Enhanced error deduction will require running a dedicated static analysis tool on the codebase (`scan-build` at some point in future?). Where applicable add more assertions to code to increase the amount of internal consistency checks. Fix a couple of small bugs found by `warn_unused_result` as well. Test Plan: Classics: ``` mysqlbuild.sh mysqltest.sh mysqltest.sh --testset=RocksDB mysqlbuild.sh --release mysqltest.sh --release mysqltest.sh --release --testset=RocksDB ``` Reviewers: alexyang, jkedgar Reviewed By: jkedgar Subscribers: MarkCallaghan, webscalesql-eng Differential Revision: https://reviews.facebook.net/D55521
1 parent aa9dd8c commit 5a82f20

File tree

7 files changed

+290
-97
lines changed

7 files changed

+290
-97
lines changed

include/my_sys.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ typedef struct my_aio_result {
152152
#define GETDATE_FIXEDLENGTH 16
153153

154154
/* defines when allocating data */
155-
extern void *my_malloc(size_t Size,myf MyFlags);
156-
extern void *my_multi_malloc(myf MyFlags, ...);
155+
extern void *my_malloc(size_t Size,myf MyFlags)
156+
__attribute__((__malloc__, __warn_unused_result__));
157+
extern void *my_multi_malloc(myf MyFlags, ...)
158+
__attribute__((__malloc__, __warn_unused_result__));
157159
extern void *my_realloc(void *oldpoint, size_t Size, myf MyFlags);
158160
extern void my_free(void *ptr);
159161
extern void *my_memdup(const void *from,size_t length,myf MyFlags);

sql/thr_malloc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ typedef struct charset_info_st CHARSET_INFO;
2222
typedef struct st_mem_root MEM_ROOT;
2323

2424
void init_sql_alloc(MEM_ROOT *root, uint block_size, uint pre_alloc_size);
25-
void *sql_alloc(size_t);
26-
void *sql_calloc(size_t);
25+
void *sql_alloc(size_t) __attribute__((__malloc__, __warn_unused_result__));
26+
void *sql_calloc(size_t) __attribute__((__malloc__, __warn_unused_result__));
2727
char *sql_strdup(const char *str);
2828
char *sql_strmake(const char *str, size_t len);
2929
void *sql_memdup(const void * ptr, size_t size);

0 commit comments

Comments
 (0)