Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Future page man de str[n]cpy

5 réponses
Avatar
Marc Boyer
Pour le strncpy, par contre, la partie NOTE et BUG a été conservée.


STRCPY(3) Linux Programmer's Manual STRCPY(3)

NAME
strcpy, strncpy - copy a string

SYNOPSIS
#include <string.h>

char *strcpy(char *dest, const char *src);

char *strncpy(char *dest, const char *src, size_t n);

DESCRIPTION
The strcpy() function copies the string pointed to by
src, including the terminating null byte ('\0'), to the
buffer pointed to by dest. The strings may not overlap,
and the destination string dest must be large enough to
receive the copy.

The strncpy() function is similar, except that at most n
bytes of src are copied. Warning: If there is no null
byte among the first n bytes of src, the string placed in
dest will not be null terminated.

If the length of src is less than n, strncpy() pads the
remainder of dest with null bytes.

A simple implementation of strncpy() might be:

char*
strncpy(char *dest, const char *src, size_t n){
size_t i;

for(i = 0 ; i < n && src[i] != '\0' ; i++)
dest[i] = src[i];
for( ; i < n ; i++)
dest[i] = '\0';

return dest;
}

RETURN VALUE
The strcpy() and strncpy() functions return a pointer to
the destination string dest.

CONFORMING TO
SVr4, 4.3BSD, C89, C99.

NOTES
Some programmers consider strncpy() to be inefficient and
error prone. If the programmer knows (i.e., includes
code to test!) that the size of dest is greater than the
length of src, then strcpy() can be used.

If there is no terminating null byte in the first n char-
acters of src, strncpy() produces an unterminated string
in dest. Programmers often prevent this mistake by forc-
ing termination as follows:

strncpy(buf, str, n);
if (n > 0)
buf[n - 1]= ' ';

BUGS
If the destination string of a strcpy() is not large
enough (that is, if the programmer was stupid or lazy,
and failed to check the size before copying) then any-
thing might happen. Overflowing fixed length strings is
a favourite cracker technique.

SEE ALSO
bcopy(3), memccpy(3), memcpy(3), memmove(3), wcscpy(3),
wcsncpy(3)
--
Si tu peux supporter d'entendre tes paroles
Travesties par des gueux pour exciter des sots
IF -- Rudyard Kipling (Trad. André Maurois)

5 réponses

Avatar
fabrizio
If there is no terminating null byte in the first n char-
acters of src, strncpy() produces an unterminated string
in dest. Programmers often prevent this mistake by forc-
ing termination as follows:

strncpy(buf, str, n);
if (n > 0)
buf[n - 1]= ' ';



Un espace ? Ce serait pas plutôt un '' ?

Avatar
Marc Boyer
Le 19-06-2007, fabrizio a écrit :
If there is no terminating null byte in the first n char-
acters of src, strncpy() produces an unterminated string
in dest. Programmers often prevent this mistake by forc-
ing termination as follows:

strncpy(buf, str, n);
if (n > 0)
buf[n - 1]= ' ';


Un espace ? Ce serait pas plutôt un '' ?


Oui, tout à fait. Un pb de copier/coller.

Marc Boyer
--
Si tu peux supporter d'entendre tes paroles
Travesties par des gueux pour exciter des sots
IF -- Rudyard Kipling (Trad. André Maurois)


Avatar
Charlie Gordon
"fabrizio" a écrit dans le message de news:
4677c45a$0$14124$
If there is no terminating null byte in the first n char-
acters of src, strncpy() produces an unterminated string
in dest. Programmers often prevent this mistake by forc-
ing termination as follows:

strncpy(buf, str, n);
if (n > 0)
buf[n - 1]= ' ';



Un espace ? Ce serait pas plutôt un '' ?


QED : Show me an instance of strncpy, I'll show you a bug !

Chqrlie.


Avatar
Antoine Leca
En news:,
Marc Boyer va escriure:

BUGS
If the destination string of a strcpy() is not large
enough (that is, if the programmer was stupid or lazy,
and failed to check the size before copying) then any-
thing might happen. Overflowing fixed length strings is
a favourite cracker technique.


Ce "BUG" s'applique aussi à strcat(), et _aussi_ à strncat() (et c'est là
l'un des dangers de strncat(), en fait, c'est le parallèle avec strncpy qui
tue.)

strncat() a un autre danger : contrairement à la quasi-totalité des
opérations sur les chaînes, le paramètre numérique n'est pas la taille de
l'objet chaîne de caractères, mais est un « nombre de caractères »,
autrement dit un strlen().
MB_LEN_MAX ou MB_CUR_MAX, quand ils sont utilisés pour dimensionner des
tableaux de caractères, (et sûrement d'autres) sont aussi dans le même cas,
mais sont quand même nettement moins fréquents.
sprintf(), et en particulier sprintf(,"%Ns",) ou sprintf(,"%*s",MAX_SIZE,)
ont conceptuellement le même défaut, mais là on a maintenant le choix
d'utiliser snprintf().


Antoine

Avatar
Charlie Gordon
"Antoine Leca" a écrit dans le message de news:
f5bc88$sg2$
En news:,
Marc Boyer va escriure:

BUGS
If the destination string of a strcpy() is not large
enough (that is, if the programmer was stupid or lazy,
and failed to check the size before copying) then any-
thing might happen. Overflowing fixed length strings is
a favourite cracker technique.


Ce "BUG" s'applique aussi à strcat(), et _aussi_ à strncat() (et c'est là
l'un des dangers de strncat(), en fait, c'est le parallèle avec strncpy
qui
tue.)

strncat() a un autre danger : contrairement à la quasi-totalité des
opérations sur les chaînes, le paramètre numérique n'est pas la taille de
l'objet chaîne de caractères, mais est un « nombre de caractères »,
autrement dit un strlen().


D'où un problème majeur lorsque ces fonctions sont utilisées à tort et à
travers par les débutants "intuitifs"

char buf[MAX_PATH];

strncpy(buf, path, MAX_PATH); /* BUG! */
strncat(buf, "", MAX_PATH); /* BUG! */
strncat(buf, filename, MAX_PATH); /* BUG! */

Et non, remplacer MAX_PATH par un sizeof() ne corrige pas ces bugs ;-)

Chqrlie.