Why Is Escaping Data Considered So Magical?

Carl Banks pavlovevidence at gmail.com
Wed Jun 30 00:49:20 EDT 2010


On Jun 28, 2:44 am, Gregory Ewing <greg.ew... at canterbury.ac.nz> wrote:
> 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*.

I can't think of any function in the standard library that observes
that convention, which inclines me to disbelieve this convention ever
really existed.  If it did, there would be functions to support it.

For that matter, I'm not really inclined to believe bytes were *that*
precious in those days.

> 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.

Well, no.  Being cognizant of the string's maximum length doesn't make
you able to pass it to printf, or system, or any other C function.

The obvious rationale behind strncpy's stupid behavior is that it's
not a string function at all, but a memory block function, that stops
at a NUL in case you don't care what's after the NUL in a block.  But
it leads you to believe it's a string function by it's name.


Carl Banks



More information about the Python-list mailing list