X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=release%2Fman3%2Fstrcpy.3;h=c52e35c2a1733de0492f1c8ded944fd2e6935b32;hb=9bab846d3fbabd0a6c37bcac1ec38299cae9683b;hp=0c2ef3ca6e89e3439318414aa19030f39c4dc61d;hpb=bb92023e2caf6dc30bd3a8f07877ec870c853e23;p=linuxjm%2FLDP_man-pages.git diff --git a/release/man3/strcpy.3 b/release/man3/strcpy.3 index 0c2ef3ca..c52e35c2 100644 --- a/release/man3/strcpy.3 +++ b/release/man3/strcpy.3 @@ -49,17 +49,15 @@ strcpy, strncpy \- 文字列をコピーする \fBchar *strncpy(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB, size_t \fP\fIn\fP\fB);\fP .fi .SH 説明 -The \fBstrcpy\fP() function copies the string pointed to by \fIsrc\fP, including -the terminating null byte (\(aq\e0\(aq), to the buffer pointed to by -\fIdest\fP. The strings may not overlap, and the destination string \fIdest\fP -must be large enough to receive the copy. \fIBeware of buffer overruns!\fP -(See BUGS.) +\fBstrcpy\fP() 関数は \fIsrc\fP が指す文字列を末尾のヌルバイト (\(aq\e0\(aq) も含めて \fIdest\fP +が指すバッファにコピーする。 二つの文字列は重なってはならない。受け側の文字列 \fIdest\fP は コピーを受け取るのに十分な大きさでなければならない。 +\fIバッファオーバーランに気を付けること!\fP (「バグ」の節を参照) .PP \fBstrncpy\fP() 関数も同様だが、 \fIsrc\fP のうち最大でも \fIn\fP バイトしかコピーされない点が異なる。 \fB警告\fP: \fIsrc\fP の最初の \fIn\fP バイトの中にヌルバイトがない場合、 \fIdest\fP に格納される文字列はヌルで終端されないことになる。 .PP -If the length of \fIsrc\fP is less than \fIn\fP, \fBstrncpy\fP() writes additional -null bytes to \fIdest\fP to ensure that a total of \fIn\fP bytes are written. +\fIsrc\fP の長さが \fIn\fP よりも短い場合、 \fBstrncpy\fP() は \fIdest\fP に追加のヌルバイトを書き込み、全部で \fIn\fP +バイトが書き込まれるようにする。 .PP \fBstrncpy\fP() の簡単な実装は以下のような感じであろう: .in +4n @@ -87,15 +85,13 @@ SVr4, 4.3BSD, C89, C99. \fBstrncpy\fP() は効率的でなく間違いを起こしやすいと考えるプログラマもいるだろう。 プログラマが \fIdest\fP の大きさが \fIsrc\fP の長さよりも 大きいことを知っている (つまり、そのことをチェックするコードを 書いている) 場合は、 \fBstrcpy()\fP を使うことができる。 -One valid (and intended) use of \fBstrncpy\fP() is to copy a C string to a -fixed\-length buffer while ensuring both that the buffer is not overflowed -and that unused bytes in the target buffer are zeroed out (perhaps to -prevent information leaks if the buffer is to be written to media or -transmitted to another process via an interprocess communication technique). +\fBstrncpy\fP() の正しい (かつ意図された) 用途は、 C 文字列の固定長バッファへのコピーを、 バッファがオーバーフローしないことと、 +宛先バッファの未使用バイトが 0 で埋められることの両方を保証しつつ行うことである。 (宛先バッファを 0 で埋めるのは、 たいていの場合、 +バッファを媒体に書き込んだり、別のプロセスにプロセス間通信を用いて送信したりした場合に情報洩れを防ぐためである)。 -If there is no terminating null byte in the first \fIn\fP bytes of \fIsrc\fP, -\fBstrncpy\fP() produces an unterminated string in \fIdest\fP. You can force -termination using something like the following: +\fIsrc\fP の最初の \fIn\fP バイトに終端のヌルバイトがない場合、 \fBstrncpy\fP() +は \fIdest\fP に終端されていない文字列を生成する。以下のようにして +強制的に終端することができる。 .in +4n .nf @@ -105,10 +101,9 @@ if (n > 0) .fi .in .PP -(Of course, the above technique ignores the fact that information contained -in \fIsrc\fP is lost in the copying to \fIdest\fP.) +(もちろん、上記の方法では、 \fIsrc\fP に入っている情報が \fIdest\fP へのコピー時に失われるという事実は無視している。) -Some systems (the BSDs, Solaris, and others) provide the following function: +いくつかのシステム (BSD、Solaris など) では以下の関数が提供されている。 size_t strlcpy(char *dest, const char *src, size_t size); @@ -116,17 +111,14 @@ Some systems (the BSDs, Solaris, and others) provide the following function: .\" "strlcpy and strlcat - consistent, safe, string copy and concatenation" .\" 1999 USENIX Annual Technical Conference .\" https://lwn.net/Articles/506530/ -This function is similar to \fBstrncpy\fP(), but it copies at most \fIsize\-1\fP -bytes to \fIdest\fP, always adds a terminating null byte, and does not pad the -target with (further) null bytes. This function fixes some of the problems -of \fBstrcpy\fP() and \fBstrncpy\fP(), but the caller must still handle the -possibility of data loss if \fIsize\fP is too small. The return value of the -function is the length of \fIsrc\fP, which allows truncation to be easily -detected: if the return value is greater than or equal to \fIsize\fP, -truncation occurred. If loss of data matters, the caller \fImust\fP either -check the arguments before the call, or test the function return value. -\fBstrlcpy\fP() is not present in glibc and is not standardized by POSIX, but -is available on Linux via the \fIlibbsd\fP library. +この関数は \fBstrncpy\fP() と同様だが、 最大でも \fIsize\-1\fP バイトしか \fIdest\fP にコピーをせず、 末尾への終端の NULL +バイトの追加が必ず行われ、 宛先バッファ (の未使用部分) への NULL バイトの書き込みが行われない。 この関数では \fBstrcpy\fP() や +\fBstrncpy\fP() の持つ問題のいくつかが修正されているが、 \fIsize\fP が小さすぎた場合にはデータが失われる問題には、 +依然として呼び出し側で対処する必要がある。 この関数の返り値は \fIsrc\fP の長さである。 これにより、 +末尾の切り詰めが行われたかを簡単に検出することができる。 返り値が \fIsize\fP 以上の場合には、 末尾の切り詰めが発生している。 +データロスが問題となる場合は、 呼び出し側で、 呼び出し前に引き数をチェックするか、 この関数の返り値を検査するかのいずれかをしなければならない。 +\fBstrlcpy\fP() は glibc には存在せず、 POSIX による標準化もされていないが、 Linux では \fIlibbsd\fP +ライブラリ経由で利用できる。 .SH バグ \fBstrcpy\fP() の受け側の文字列が十分な大きさでない場合、何が起こるかわからない。 固定長文字列を溢れさせるのは、マシンの制御を掌中に収めるために クラッカーが好んで使うテクニックである。 @@ -137,6 +129,6 @@ is available on Linux via the \fIlibbsd\fP library. \fBbcopy\fP(3), \fBmemccpy\fP(3), \fBmemcpy\fP(3), \fBmemmove\fP(3), \fBstpcpy\fP(3), \fBstpncpy\fP(3), \fBstrdup\fP(3), \fBstring\fP(3), \fBwcscpy\fP(3), \fBwcsncpy\fP(3) .SH この文書について -この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.50 の一部 +この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。