Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
21.18 is a safe strncpy function call non-compliant?
#1
I have a philosophical question.


Example code:

void foo() {
    char buf[128];
    strncpy(buf, "hello", 128);
}

The strncpy call does not have any undefined behavior. It will write "hello" in the buffer. Writing 6 bytes in a 128 byte buffer is not undefined behavior.

Does this code then violate rule 21.18? The third argument is larger than the size of the string literal.

Reading the amplification, it seems to me the code in non-compliant.

Reading the rationale, the point of this rule is to avoid buffer overflows. Since there is no buffer overflow does it mean the code is compliant?
Reply
#2
ping? I wrote "philosophical" because I would like to understand the philosophy.
There is a customer that thinks this is a false positive.. and technically I agree the code is safe however the wording in the PDF makes me think the code is non-compliant.
I look forward to the answer.
Reply
#3
The amplification of Rule 21.18 states that the size parameter shall be no greater than the size of the smallest object passed to the function through a pointer parameter.

In your example, the smallest object is six characters - therefore passing a size of 128 is non-compliant with the Rule.
Posted by and on behalf of the MISRA C Working Group
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)