Add parameter check to the message queue creation function

This commit is contained in:
pillar 2024-05-10 14:48:35 +08:00
parent 75f41723cb
commit dbb3c315f4
2 changed files with 10 additions and 2 deletions

View File

@ -112,6 +112,8 @@ extern "C" {
#define RT_MUTEX_VALUE_MAX RT_UINT16_MAX /**< Maximum number of mutex .value */
#define RT_MUTEX_HOLD_MAX RT_UINT8_MAX /**< Maximum number of mutex .hold */
#define RT_MB_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of mailbox .entry */
#define RT_MQ_MSG_SIZE_MAX RT_UINT16_MAX /**< Maximum number of message queue .msg_size */
#define RT_MQ_MAX_MSGS_MAX RT_UINT16_MAX /**< Maximum number of message queue .max_msgs */
#define RT_MQ_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of message queue .entry */
/* Common Utilities */

View File

@ -3091,10 +3091,12 @@ rt_err_t rt_mq_init(rt_mq_t mq,
struct rt_mq_message *head;
rt_base_t temp;
register rt_size_t msg_align_size;
rt_size_t correct_max_msgs;
/* parameter check */
RT_ASSERT(mq != RT_NULL);
RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
RT_ASSERT((msg_size <= RT_MQ_MSG_SIZE_MAX));
/* initialize object */
rt_object_init(&(mq->parent.parent), RT_Object_Class_MessageQueue, name);
@ -3111,12 +3113,14 @@ rt_err_t rt_mq_init(rt_mq_t mq,
/* get correct message size */
msg_align_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
mq->msg_size = msg_size;
mq->max_msgs = pool_size / (msg_align_size + sizeof(struct rt_mq_message));
if (0 == mq->max_msgs)
/* set the max of messages */
correct_max_msgs = pool_size / (msg_align_size + sizeof(struct rt_mq_message));
if ((0 == correct_max_msgs) || (correct_max_msgs > RT_MQ_MAX_MSGS_MAX))
{
return -RT_EINVAL;
}
mq->max_msgs = correct_max_msgs;
/* initialize message list */
mq->msg_queue_head = RT_NULL;
@ -3228,6 +3232,8 @@ rt_mq_t rt_mq_create(const char *name,
rt_base_t temp;
register rt_size_t msg_align_size;
RT_ASSERT((msg_size <= RT_MQ_MSG_SIZE_MAX));
RT_ASSERT((max_msgs <= RT_MQ_MAX_MSGS_MAX));
RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
RT_DEBUG_NOT_IN_INTERRUPT;