Secure coding: Difference between revisions

Content deleted Content added
Wiki.0905 (talk | contribs)
m Added hyperlink
Buffer-overflow prevention: terminate last character with NUL
Line 24:
// copy a maximum of BUF_SIZE bytes
strncpy(dst, user_input, BUF_SIZE);
// set the last character in the buffer to NUL,
dst[BUF_SIZE -1] = '\0';
}
</syntaxhighlight>Another secure alternative is to dynamically allocate memory on the heap using [[malloc]].<syntaxhighlight lang="c++">