Skip to content

Commit 31438b2

Browse files
committed
Fix q_remove_tail and q_remove_head
Fixed the error at q_remove_tail and q_remove_head.The previous function check whether input of character pointer is empty at the start of the function.However this will cause the function not remove the node when input is setting wrong.Therefore, I move the checking after the list delete function. Change-Id: I93e0ab7530a1cc6dbecfed444ac2e714eb7cef29
1 parent fd64306 commit 31438b2

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

queue.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,17 @@ bool q_insert_tail(struct list_head *head, char *s)
9090
/* Remove an element from head of queue */
9191
element_t *q_remove_head(struct list_head *head, char *sp, size_t bufsize)
9292
{
93-
if (!head || list_empty(head) || !sp) {
93+
if (!head || list_empty(head)) {
9494
return NULL;
9595
}
9696

9797
element_t *first = list_first_entry(head, element_t, list);
9898
list_del(&first->list);
9999

100+
if (!sp) {
101+
return NULL;
102+
}
103+
100104
size_t len = strlen(first->value);
101105
size_t copy_size = (len < bufsize - 1) ? len : (bufsize - 1);
102106

@@ -109,13 +113,17 @@ element_t *q_remove_head(struct list_head *head, char *sp, size_t bufsize)
109113
/* Remove an element from tail of queue */
110114
element_t *q_remove_tail(struct list_head *head, char *sp, size_t bufsize)
111115
{
112-
if (!head || list_empty(head) || !sp) {
116+
if (!head || list_empty(head)) {
113117
return NULL;
114118
}
115119

116120
element_t *tail = list_last_entry(head, element_t, list);
117121
list_del(&tail->list);
118122

123+
if (!sp) {
124+
return NULL;
125+
}
126+
119127
size_t len = strlen(tail->value);
120128
size_t copy_size = (len < bufsize - 1) ? len : (bufsize - 1);
121129

0 commit comments

Comments
 (0)