Skip to content

Commit 2e2f02e

Browse files
yechun1jren1
authored andcommitted
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 <yechunliangcn@163.com>
1 parent 9aa9a77 commit 2e2f02e

File tree

3 files changed

+3
-5
lines changed

3 files changed

+3
-5
lines changed

devicemodel/core/vmmapi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ vm_open(const char *name)
118118
int error, retry = 10;
119119
uuid_t vm_uuid;
120120

121-
ctx = malloc(sizeof(struct vmctx) + strlen(name) + 1);
121+
ctx = calloc(1, sizeof(struct vmctx) + strlen(name) + 1);
122122
assert(ctx != NULL);
123123
assert(devfd == -1);
124124

devicemodel/hw/platform/rtc.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,9 +1105,8 @@ vrtc_init(struct vmctx *ctx, int local_time)
11051105
time_t curtime;
11061106
struct inout_port rtc_addr, rtc_data;
11071107

1108-
vrtc = malloc(sizeof(struct vrtc));
1108+
vrtc = calloc(1, sizeof(struct vrtc));
11091109
assert(vrtc != NULL);
1110-
memset(vrtc, 0, sizeof(struct vrtc));
11111110
vrtc->vm = ctx;
11121111
ctx->vrtc = vrtc;
11131112

devicemodel/tools/acrntrace/acrntrace.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,11 @@ int main(int argc, char *argv[])
340340

341341
/* how many cpus */
342342
pcpu_num = get_cpu_num();
343-
reader = malloc(sizeof(reader_struct) * pcpu_num);
343+
reader = calloc(1, sizeof(reader_struct) * pcpu_num);
344344
if (!reader) {
345345
pr_err("Failed to allocate reader memory\n");
346346
exit(EXIT_FAILURE);
347347
}
348-
memset(reader, 0, sizeof(reader_struct) * pcpu_num);
349348

350349
/* create dir for trace file */
351350
if (create_trace_file_dir(trace_file_dir)) {

0 commit comments

Comments
 (0)