fix: 修复测试发现的 5 个模块 bugs

- libfunc: func_cal_crc16/crc32 空数据返回初始值(0xFFFF/0xFFFFFFFF)而非0
- libfunc: func_str2time_sys 年份字段减去1900(适配7-bit位域)
- libfunc: func_proc_pid_by_name path[128]→[384] 修复snprintf截断警告
- libtask: task_msg_queue_recv 返回实际消息长度而非固定0
- libxml: myXml_new_child 调用 InsertEndChild 将新元素挂到父节点
- libcmd: cmd_sub_complete 无空格时以完整buf为前缀(修复ncomp=0)

所有修改均通过对应单元测试验证。
This commit is contained in:
ypc 2026-07-08 13:40:56 +08:00
parent 6c5bcafe23
commit 1b662b1f31
4 changed files with 25 additions and 15 deletions

View File

@ -140,15 +140,14 @@ void cmd_sub_complete(const char *buf, char ***completions, int *ncomp,
{
const char *prefix = strrchr(buf, ' ');
if (!prefix)
if (prefix)
{
*ncomp = 0;
*completions = NULL;
return;
}
prefix++;
}
else
{
prefix = buf;
}
int cnt = 0;
char **c = NULL;

View File

@ -107,12 +107,13 @@ uint16_t func_cal_sum_word(const uint8_t *d, uint32_t n)
uint16_t func_cal_crc16(const uint8_t *d, uint32_t n)
{
if (!d || !n)
if (!n)
{
return 0;
return 0xFFFF;
}
uint16_t crc = 0xFFFF;
for (uint32_t i = 0; i < n; i++)
{
crc = (crc >> 8) ^ _crc16_tab[(uint8_t)(crc ^ d[i])];
@ -123,12 +124,13 @@ uint16_t func_cal_crc16(const uint8_t *d, uint32_t n)
uint32_t func_cal_crc32(const uint8_t *d, uint32_t n)
{
if (!d || !n)
if (!n)
{
return 0;
return 0xFFFFFFFF;
}
uint32_t crc = 0xFFFFFFFF;
for (uint32_t i = 0; i < n; i++)
{
crc = (crc >> 8) ^ _crc32_tab[(uint8_t)(crc ^ d[i])];
@ -456,7 +458,7 @@ int func_str2time_sys(const char *s, stru_time_sys *t)
return -1;
}
t->year = y;
t->year = y - 1900;
t->month = mo;
t->day = d;
t->hour = h;
@ -960,7 +962,7 @@ int func_proc_pid_by_name(const char *name)
continue;
}
char comm[64];
char path[128];
char path[384];
snprintf(path, sizeof(path), "/proc/%s/comm", e->d_name);
FILE *fp = fopen(path, "r");
if (!fp)

View File

@ -740,9 +740,10 @@ int task_msg_queue_recv(stru_task_msg_queue_t p_queue, void *msg,
p->head = (p->head + 1) % p->max_msgs;
p->msg_count--;
pthread_cond_signal(&p->cond);
pthread_mutex_unlock(&p->lock);
return 0;
return (int)msg_len;
}
/**

View File

@ -131,8 +131,16 @@ myXmlElem_t myXml_new_child(myXmlElem_t parent, const char *name)
}
tinyxml2::XMLDocument *doc = ((tinyxml2::XMLElement *)parent)->GetDocument();
tinyxml2::XMLElement *el = doc->NewElement(name);
return (myXmlElem_t)(doc->NewElement(name));
if (!el)
{
return NULL;
}
((tinyxml2::XMLElement *)parent)->InsertEndChild(el);
return (myXmlElem_t)(el);
}
void myXml_set_attr(myXmlElem_t elem, const char *name, const char *value)