How it works...
In this recipe, everything is the same as the previous recipe. The point of difference is that we invoke the strncpy function. When this function is invoked, only the strsize number of bytes from the administrator text is assigned to the name member of the user1 structure. Because strsize contains the maximum length of the name member, no buffer overflow will occur in this case.
Finally, we check if the null character, \0, exists as the last character in the name member. If not, then the null character is added at its end to terminate the string. On displaying the information of the user, we see that because the length of the name member is 10, only the first 9 characters from the text administrator are assigned to the name ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access