ToC added, several markup issues fixed
This commit is contained in:
parent
a968cc7369
commit
114f54b808
4 changed files with 35 additions and 36 deletions
|
@ -124,10 +124,10 @@ construct a string in a statically-sized buffer. (If the buffer
|
|||
size is allocated on the heap, consider use
|
||||
`asprintf` instead.)
|
||||
|
||||
[subs="quotes"]
|
||||
[source,c]
|
||||
----
|
||||
include::snippets/C-String-Functions-snprintf.adoc[]
|
||||
|
||||
|
||||
----
|
||||
|
||||
The second argument to the `snprintf` call
|
||||
|
@ -154,10 +154,10 @@ invariant. After the loop, the result string is in the
|
|||
.Repeatedly writing to a buffer using `snprintf`
|
||||
====
|
||||
|
||||
[subs="quotes"]
|
||||
[source,c]
|
||||
----
|
||||
include::snippets/C-String-Functions-snprintf-incremental.adoc[]
|
||||
|
||||
|
||||
----
|
||||
|
||||
====
|
||||
|
@ -189,10 +189,10 @@ function (see <<ex-Defensive_Coding-C-String-Functions-format-Attribute>>).
|
|||
.The `format` function attribute
|
||||
====
|
||||
|
||||
[subs="quotes"]
|
||||
[source,c]
|
||||
----
|
||||
include::snippets/C-String-Functions-format.adoc[]
|
||||
|
||||
|
||||
----
|
||||
|
||||
====
|
||||
|
@ -204,19 +204,19 @@ The `strncpy` function does not ensure that
|
|||
the target buffer is null-terminated. A common idiom for
|
||||
ensuring NUL termination is:
|
||||
|
||||
[subs="quotes"]
|
||||
[source,c]
|
||||
----
|
||||
include::snippets/C-String-Functions-strncpy.adoc[]
|
||||
|
||||
|
||||
----
|
||||
|
||||
Another approach uses the `strncat`
|
||||
function for this purpose:
|
||||
|
||||
[subs="quotes"]
|
||||
[source,c]
|
||||
----
|
||||
include::snippets/C-String-Functions-strncat-as-strncpy.adoc[]
|
||||
|
||||
|
||||
----
|
||||
|
||||
[[sect-Defensive_Coding-C-Libc-strncat]]
|
||||
|
@ -236,20 +236,20 @@ emulation described in <<sect-Defensive_Coding-C-Libc-strncpy>>.
|
|||
To implement a length-checked string append, you can use an
|
||||
approach similar to <<ex-Defensive_Coding-C-String-Functions-snprintf-incremental>>:
|
||||
|
||||
[subs="quotes"]
|
||||
[source,c]
|
||||
----
|
||||
include::snippets/C-String-Functions-strncat-emulation.adoc[]
|
||||
|
||||
|
||||
----
|
||||
|
||||
In many cases, including this one, the string concatenation
|
||||
can be avoided by combining everything into a single format
|
||||
string:
|
||||
|
||||
[subs="quotes"]
|
||||
[source,c]
|
||||
----
|
||||
include::snippets/C-String-Functions-strncat-merged.adoc[]
|
||||
|
||||
|
||||
----
|
||||
|
||||
But you should must not dynamically construct format strings
|
||||
|
@ -261,8 +261,7 @@ It is not possible to use format strings like
|
|||
you use separate buffers. `snprintf` does
|
||||
not support overlapping source and target strings.
|
||||
|
||||
====== `strlcpy` and
|
||||
`strlcat`
|
||||
====== `strlcpy` and `strlcat`
|
||||
|
||||
Some systems support `strlcpy` and
|
||||
`strlcat` functions which behave this way,
|
||||
|
@ -280,9 +279,8 @@ described in <<sect-Defensive_Coding-C-Libc-strncat>>.
|
|||
ISO C11 adds another set of length-checking functions, but GNU
|
||||
libc currently does not implement them.
|
||||
|
||||
====== Other `strn`pass:attributes[{blank}]* and
|
||||
`stpn`pass:attributes[{blank}]* functions
|
||||
====== Other `strn*` and `stpn*` functions
|
||||
|
||||
GNU libc contains additional functions with different variants
|
||||
of length checking. Consult the documentation before using
|
||||
them to find out what the length actually means.
|
||||
them to find out what the length actually means.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue