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.
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)
Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
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 ' ' ?
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:
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 ' ' ?
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)
Le 19-06-2007, fabrizio <watch.the@stars.mw> 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)
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)
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.
"fabrizio" <watch.the@stars.mw> a écrit dans le message de news:
4677c45a$0$14124$426a74cc@news.free.fr...
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 !
"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.
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
En news:slrnf7ff2d.3mv.Marc.Boyer@localhost.localdomain,
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().
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
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"
Et non, remplacer MAX_PATH par un sizeof() ne corrige pas ces bugs ;-)
Chqrlie.
"Antoine Leca" <root@localhost.invalid> a écrit dans le message de news:
f5bc88$sg2$1@shakotay.alphanet.ch...
En news:slrnf7ff2d.3mv.Marc.Boyer@localhost.localdomain,
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"
"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"