From bb63fe6fc3a4d5f0f0639ad7712971da0ee607c4 Mon Sep 17 00:00:00 2001 From: Darek Stojaczyk Date: Mon, 22 Jul 2019 14:13:28 +0200 Subject: [PATCH] blobstore: don't realloc any memory under scan-build Scan-build has a real issue with reallocs. The original error from latest version of scan-build is rather complicated, but it can be greatly simplified with the following change: > diff --git a/lib/blob/blobstore.c b/lib/blob/blobstore.c > index 7580c9dd2..6a594edf3 100644 > --- a/lib/blob/blobstore.c > +++ b/lib/blob/blobstore.c > @@ -1147,8 +1147,9 @@ > _spdk_blob_persist_clear_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int > } else if (blob->active.num_clusters != blob->active.cluster_array_size) { > tmp = realloc(blob->active.clusters, sizeof(uint64_t) * blob->active.num_clusters); > assert(tmp != NULL); > - blob->active.clusters = tmp; > - blob->active.cluster_array_size = blob->active.num_clusters; > + ctx->blob->active.clusters = tmp; > + assert(ctx->blob->active.clusters[0] != 14213); > + ctx->blob->active.cluster_array_size = ctx->blob->active.num_clusters; > } > > _spdk_blob_persist_complete(seq, ctx, bserrno); > ``` Scan-build will then complain: blobstore.c:1151:10: warning: Use of memory after it is freed assert(ctx->blob->active.clusters[0] != 14213); Asserting blob == ctx->blob, blob->active.clusters == ctx->..., or even tmp != blob->active.clusters doesn't work, so use the last resort scan-build weapon - #ifdef __clang_analyzer__. The realloc in this case is just down-sizing a buffer to save some memory. For scan-build, just don't do it. This finally silences all scan-build false positives. Change-Id: Ib88ea145370f5035eedd2412e98ee61f96ad1915 Signed-off-by: Darek Stojaczyk Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/462868 Tested-by: SPDK CI Jenkins Reviewed-by: Tomasz Zawadzki Reviewed-by: Ben Walker Reviewed-by: Shuhei Matsumoto --- lib/blob/blobstore.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/blob/blobstore.c b/lib/blob/blobstore.c index 7580c9dd2..444ad896f 100644 --- a/lib/blob/blobstore.c +++ b/lib/blob/blobstore.c @@ -1127,7 +1127,6 @@ _spdk_blob_persist_clear_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int struct spdk_blob_persist_ctx *ctx = cb_arg; struct spdk_blob *blob = ctx->blob; struct spdk_blob_store *bs = blob->bs; - void *tmp; size_t i; /* Release all clusters that were truncated */ @@ -1145,9 +1144,14 @@ _spdk_blob_persist_clear_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int blob->active.clusters = NULL; blob->active.cluster_array_size = 0; } else if (blob->active.num_clusters != blob->active.cluster_array_size) { +#ifndef __clang_analyzer__ + void *tmp; + + /* scan-build really can't figure reallocs, workaround it */ tmp = realloc(blob->active.clusters, sizeof(uint64_t) * blob->active.num_clusters); assert(tmp != NULL); blob->active.clusters = tmp; +#endif blob->active.cluster_array_size = blob->active.num_clusters; }