From 956f1e2387a807a2e3ec25e75d04050c705ab3b9 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 7 Jun 2016 09:17:52 +0200 Subject: [PATCH 1/2] coverity: add user model The static analysis engine coverity allows for user models overriding how it treats functions when analyzing code. Like this, one can greatly reduce the rate of false positives and thus make it easier to spot actual errors. Add a user model that overrides function models for `git_buf_len` and `git_vector_insert`, which together amount for a majority of false positives. --- script/user_model.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 script/user_model.c diff --git a/script/user_model.c b/script/user_model.c new file mode 100644 index 000000000..3c00b6984 --- /dev/null +++ b/script/user_model.c @@ -0,0 +1,37 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +void *realloc(void *ptr, size_t size); +size_t strlen(const char *s); + +typedef struct git_vector { + void **contents; + size_t length; +} git_vector; + +typedef struct git_buf { + char *ptr; + size_t asize, size; +} git_buf; + +int git_vector_insert(git_vector *v, void *element) +{ + if (!v) + __coverity_panic__(); + + v->contents = realloc(v->contents, ++v->length); + if (!v->contents) + __coverity_panic__(); + v->contents[v->length] = element; + + return 0; +} + +int git_buf_len(const struct git_buf *buf) +{ + return strlen(buf->ptr); +} From 4d8fe1cda0175bdedb4a868910e9a24c37d72d74 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 7 Jun 2016 09:20:35 +0200 Subject: [PATCH 2/2] coverity: model functions printing into git_buf The `git_buf` structure seems to be too complicated to correctly grasp for Coverity. As such, add simpler models trying to guide Coverity and remove false positives related to these functions. --- script/user_model.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/script/user_model.c b/script/user_model.c index 3c00b6984..a933d735c 100644 --- a/script/user_model.c +++ b/script/user_model.c @@ -6,8 +6,11 @@ */ void *realloc(void *ptr, size_t size); +void *memmove(void *dest, const void *src, size_t n); size_t strlen(const char *s); +typedef struct va_list_str *va_list; + typedef struct git_vector { void **contents; size_t length; @@ -35,3 +38,38 @@ int git_buf_len(const struct git_buf *buf) { return strlen(buf->ptr); } + +int git_buf_vprintf(git_buf *buf, const char *format, va_list ap) +{ + char ch, *s; + size_t len; + + __coverity_string_null_sink__(format); + __coverity_string_size_sink__(format); + + ch = *format; + ch = *(char *)ap; + + buf->ptr = __coverity_alloc__(len); + __coverity_writeall__(buf->ptr); + buf->size = len; + + return 0; +} + +int git_buf_put(git_buf *buf, const char *data, size_t len) +{ + buf->ptr = __coverity_alloc__(buf->size + len + 1); + memmove(buf->ptr + buf->size, data, len); + buf->size += len; + buf->ptr[buf->size + len] = 0; + return 0; +} + +int git_buf_set(git_buf *buf, const void *data, size_t len) +{ + buf->ptr = __coverity_alloc__(len + 1); + memmove(buf->ptr, data, len); + buf->size = len + 1; + return 0; +}