Skip to content

Commit 7e4b4c2

Browse files
lyan3jren1
authored andcommitted
Remove ASSERT in lib functions
Replace ASSERT in lib functions with error message print and return a value indicating error to allow the caller of lib functions to handle the error. Change-Id: If166484238dc0734041adfdbb19a5b374c044e33 Signed-off-by: Yan, Like <like.yan@intel.com>
1 parent cc2256d commit 7e4b4c2

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

hypervisor/lib/mem_mgt.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ void *malloc(unsigned int num_bytes)
266266
}
267267

268268
/* Check if memory allocation is successful */
269-
ASSERT(memory != NULL, "");
269+
if (memory == NULL)
270+
pr_err("%s: failed to alloc 0x%x Bytes", __func__, num_bytes);
270271

271272
/* Return memory pointer to caller */
272273
return memory;
@@ -280,7 +281,8 @@ void *alloc_pages(unsigned int page_num)
280281
memory = allocate_mem(&Paging_Memory_Pool, page_num * CPU_PAGE_SIZE);
281282

282283
/* Check if memory allocation is successful */
283-
ASSERT(memory != NULL, "");
284+
if (memory == NULL)
285+
pr_err("%s: failed to alloc %d pages", __func__, page_num);
284286

285287
return memory;
286288
}

hypervisor/lib/memcpy.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
*
5555
* OUTPUTS
5656
*
57-
* void * pointer to destination address
57+
* void * pointer to destination address if successful,
58+
* or else return null.
5859
*
5960
***********************************************************************/
6061
void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen)
@@ -63,17 +64,21 @@ void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen)
6364
uint8_t *dest8;
6465
uint8_t *src8;
6566

67+
if (slen == 0 || dmax == 0 || dmax < slen) {
68+
pr_err("%s: invalid src, dest buffer or length.", __func__);
69+
return NULL;
70+
}
71+
72+
if ((d > s && d <= s + slen - 1)
73+
|| (d < s && s <= d + dmax - 1)) {
74+
pr_err("%s: overlap happened.", __func__);
75+
return NULL;
76+
}
77+
6678
/*same memory block, no need to copy*/
6779
if (d == s)
6880
return d;
6981

70-
ASSERT((slen != 0) && (dmax != 0) && (dmax >= slen),
71-
"invalid slen or dmax.");
72-
73-
ASSERT(((d > s) && (d > s + slen - 1))
74-
|| ((d < s) && (s > d + dmax - 1)),
75-
"overlap happened.");
76-
7782
dest8 = (uint8_t *)d;
7883
src8 = (uint8_t *)s;
7984

hypervisor/lib/strcpy.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ char *strcpy_s(char *d, size_t dmax, const char *s)
6363
size_t dest_avail;
6464
uint64_t overlap_guard;
6565

66-
ASSERT(s != NULL, "invalid input s.");
67-
ASSERT((d != NULL) && (dmax != 0), "invalid input d or dmax.");
66+
if (s == NULL || d == NULL || dmax == 0) {
67+
pr_err("%s: invalid src, dest buffer or length.", __func__);
68+
return NULL;
69+
}
6870

6971
if (s == d)
7072
return d;
@@ -75,7 +77,11 @@ char *strcpy_s(char *d, size_t dmax, const char *s)
7577
dest_base = d;
7678

7779
while (dest_avail > 0) {
78-
ASSERT(overlap_guard != 0, "overlap happened.");
80+
if (overlap_guard == 0) {
81+
pr_err("%s: overlap happened.", __func__);
82+
*(--d) = '\0';
83+
return NULL;
84+
}
7985

8086
*d = *s;
8187
if (*d == '\0')
@@ -87,7 +93,7 @@ char *strcpy_s(char *d, size_t dmax, const char *s)
8793
overlap_guard--;
8894
}
8995

90-
ASSERT(false, "dest buffer has no enough space.");
96+
pr_err("%s: dest buffer has no enough space.", __func__);
9197

9298
/*
9399
* to avoid a string that is not

hypervisor/lib/strncpy.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,15 @@ char *strncpy_s(char *d, size_t dmax, const char *s, size_t slen)
6666
size_t dest_avail;
6767
uint64_t overlap_guard;
6868

69-
ASSERT((d != NULL) && (s != NULL), "invlaid input d or s");
70-
ASSERT((dmax != 0) && (slen != 0), "invlaid input dmax or slen");
69+
if (d == NULL || s == NULL) {
70+
pr_err("%s: invlaid src or dest buffer", __func__);
71+
return NULL;
72+
}
73+
74+
if (dmax == 0 || slen == 0) {
75+
pr_err("%s: invlaid length of src or dest buffer", __func__);
76+
return NULL;
77+
}
7178

7279
if (d == s)
7380
return d;
@@ -78,7 +85,11 @@ char *strncpy_s(char *d, size_t dmax, const char *s, size_t slen)
7885
dest_avail = dmax;
7986

8087
while (dest_avail > 0) {
81-
ASSERT(overlap_guard != 0, "overlap happened.");
88+
if (overlap_guard == 0) {
89+
pr_err("%s: overlap happened.", __func__);
90+
*(--d) = '\0';
91+
return NULL;
92+
}
8293

8394
if (slen == 0) {
8495
*d = '\0';
@@ -96,7 +107,7 @@ char *strncpy_s(char *d, size_t dmax, const char *s, size_t slen)
96107
overlap_guard--;
97108
}
98109

99-
ASSERT(false, "dest buffer has no enough space.");
110+
pr_err("%s: dest buffer has no enough space.", __func__);
100111

101112
/*
102113
* to avoid a string that is not

0 commit comments

Comments
 (0)