From f48e5d1a02b69d6f13172107b4bc8e78df5d4483 Mon Sep 17 00:00:00 2001 From: yechunliang Date: Thu, 5 Apr 2018 19:57:13 +0800 Subject: [PATCH] replace malloc with calloc malloc: allocate a block of memory, the contents of the block are undefined. calloc: allocate a block of memory for an array of num elements and initializes all its bits to zero. Signed-off-by: yechunliang --- core/vmmapi.c | 2 +- hw/platform/rtc.c | 3 +-- tools/acrntrace/acrntrace.c | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/core/vmmapi.c b/core/vmmapi.c index ffc3dc750..16efd8716 100644 --- a/core/vmmapi.c +++ b/core/vmmapi.c @@ -118,7 +118,7 @@ vm_open(const char *name) int error, retry = 10; uuid_t vm_uuid; - ctx = malloc(sizeof(struct vmctx) + strlen(name) + 1); + ctx = calloc(1, sizeof(struct vmctx) + strlen(name) + 1); assert(ctx != NULL); assert(devfd == -1); diff --git a/hw/platform/rtc.c b/hw/platform/rtc.c index 52650ea80..a58839151 100644 --- a/hw/platform/rtc.c +++ b/hw/platform/rtc.c @@ -1105,9 +1105,8 @@ vrtc_init(struct vmctx *ctx, int local_time) time_t curtime; struct inout_port rtc_addr, rtc_data; - vrtc = malloc(sizeof(struct vrtc)); + vrtc = calloc(1, sizeof(struct vrtc)); assert(vrtc != NULL); - memset(vrtc, 0, sizeof(struct vrtc)); vrtc->vm = ctx; ctx->vrtc = vrtc; diff --git a/tools/acrntrace/acrntrace.c b/tools/acrntrace/acrntrace.c index 2c9f7e4f3..5817e7596 100644 --- a/tools/acrntrace/acrntrace.c +++ b/tools/acrntrace/acrntrace.c @@ -340,12 +340,11 @@ int main(int argc, char *argv[]) /* how many cpus */ pcpu_num = get_cpu_num(); - reader = malloc(sizeof(reader_struct) * pcpu_num); + reader = calloc(1, sizeof(reader_struct) * pcpu_num); if (!reader) { pr_err("Failed to allocate reader memory\n"); exit(EXIT_FAILURE); } - memset(reader, 0, sizeof(reader_struct) * pcpu_num); /* create dir for trace file */ if (create_trace_file_dir(trace_file_dir)) {