Why Is Escaping Data Considered So Magical?

Gregory Ewing greg.ewing at canterbury.ac.nz
Mon Jun 28 05:44:53 EDT 2010


Carl Banks wrote:

> Indeed, strncpy does not copy that final NUL if it's at or beyond the
> nth element.  Probably the most mind-bogglingly stupid thing about the
> standard C library, which has lots of mind-boggling stupidity.

I don't think it was as stupid as that back when C was
designed. Every byte of memory was precious in those days,
and if you had, say, 10 bytes allocated for a string, you
wanted to be able to use all 10 of them for useful data.

So the convention was that a NUL byte was used to mark
the end of the string *if it didn't fill all the available
space*. Functions such as strncpy and snprintf are designed
for use with strings that follow this convention. Proper
usage requires being cognizant of the maximum length and
using appropriate length-limited functions for all operations
on such strings.

-- 
Greg



More information about the Python-list mailing list