1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2010-11-17     Bernard      first version
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <finsh.h>
14 #include <sys/errno.h>
15 
16 static int errors = 0;
merror(const char * msg)17 static void merror(const char *msg)
18 {
19     ++errors;
20     printf("Error: %s\n", msg);
21 }
22 
libc_mem(void)23 int libc_mem(void)
24 {
25     void *p;
26     int save;
27 
28     errno = 0;
29 
30     p = malloc(-1);
31     save = errno;
32 
33     if (p != NULL)
34         merror("malloc (-1) succeeded.");
35 
36     if (p == NULL && save != ENOMEM)
37         merror("errno is not set correctly");
38 
39     p = malloc(10);
40     if (p == NULL)
41         merror("malloc (10) failed.");
42 
43     /* realloc (p, 0) == free (p).  */
44     p = realloc(p, 0);
45     if (p != NULL)
46         merror("realloc (p, 0) failed.");
47 
48     p = malloc(0);
49     if (p == NULL)
50     {
51         printf("malloc(0) returns NULL\n");
52     }
53 
54     p = realloc(p, 0);
55     if (p != NULL)
56         merror("realloc (p, 0) failed.");
57 
58     return errors != 0;
59 }
60 FINSH_FUNCTION_EXPORT(libc_mem, memory test for libc);
61