This is my very first question here, I think. I wrote my C code trying to respect "-Wall -Wextra -Werror -ansi -pedantic", but I can be flexible.
I wrote a function with the following prototype :
int messagequeue_add(messagequeue_t *p_queue, char* p_msg, const size_t p_size)
I use "realloc(p_msg, XXX)" in this function. Thus, it is not supposed to receive a string literal pointer, but only a dynamic pointer. How to forbid string literal pointers and allow string dynamic pointers only, in the function prototype, if possible with standard C89 or C99
I already met functions disallowing literal strings in their prototype arguments (error/warning at compile time), but I can not remember where and how it was defined.
I tried to Google a lot, went to my whole paper-library, this question is too specific and it was impossible to find the needle in the haystack. I'm quite experienced, but I do need help, this time.
Full function code is :
int messagequeue_add(messagequeue_t *p_queue, char* p_msg, const size_t p_size) {
messagequeueitem_t l_messagequeueitem;
/* Parameters sanity checks for early return */
if (!p_queue) {
warnx("%s:%s():%d (%s@%s): p_queue can not be NULL",
__FILE__, __func__, __LINE__, __DATE__, __TIME__);
return -1;
}
if (!(*p_queue)) {
warnx("%s:%s():%d (%s@%s): *p_queue can not be NULL",
__FILE__, __func__, __LINE__, __DATE__, __TIME__);
return -1;
}
if ((!p_msg)&&(p_size)) {
warnx("%s:%s():%d (%s@%s): p_msg can not be NULL with p_size>0",
__FILE__, __func__, __LINE__, __DATE__, __TIME__);
return -1;
}
ASSERT(p_queue);
ASSERT(*p_queue);
ASSERT( ((NULL==(*p_queue)->head)&&(NULL==(*p_queue)->tail))
||((NULL!=(*p_queue)->head)&&(NULL!=(*p_queue)->tail)));
ASSERT((p_msg)||((!p_msg)&&(!p_size)));
/* Create messagequeueitem_t structure */
if (NULL == ((l_messagequeueitem) = (messagequeueitem_t)malloc(sizeof(struct messagequeueitem_s)))) {
warn("%s:%s():%d (%s@%s): malloc failed",
__FILE__, __func__, __LINE__, __DATE__, __TIME__);
return -1;
}
ASSERT(l_messagequeueitem);
l_messagequeueitem->size = p_size;
l_messagequeueitem->next=NULL;
/* Do not create a copy of the message but take ownership of the allocated
* contents. */
/* Resize the memory allocation to fit in the case sizeof(p_msg) != p_size */
DBG_PRINTF("p_msg=%p",p_msg);
if (NULL == ( l_messagequeueitem->msg = realloc( p_msg, l_messagequeueitem->size))) {
warn("%s:%s():%d (%s@%s): realloc failed",
__FILE__, __func__, __LINE__, __DATE__, __TIME__);
free(l_messagequeueitem);
return -1;
}
if ((*p_queue)->tail)
(*p_queue)->tail->next = l_messagequeueitem;
else
(*p_queue)->head = l_messagequeueitem;
(*p_queue)->tail = l_messagequeueitem;
return 0;
}