Do You Coding?

[library] strlcat ๊ตฌํ˜„ํ•˜๊ธฐ ๋ณธ๋ฌธ

CS & Engineering/C

[library] strlcat ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿ“Œ ๋งค๋‰ด์–ผ (Linux)

๋”๋ณด๊ธฐ

NAME
     strlcat — size-bounded string concatenation

: size๋กœ ์ œํ•œํ•œ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ

 

SYNOPSIS
     #include <string.h>
     size_t strlcat(char *dst, const char *src, size_t size);

 

DESCRIPTION
     The strlcpy() and strlcat() functions copy and concatenate strings respectively.  They are deโ€
     signed to be safer, more consistent, and less error prone replacements for strncpy(3) and
     strncat(3).  Unlike those functions, strlcpy() and strlcat() take the full size of the buffer
     (not just the length) and guarantee to NUL-terminate the result (as long as size is larger than
     0 or, in the case of strlcat(), as long as there is at least one byte free in dst).  Note that a
     byte for the NUL should be included in size.  Also note that strlcpy() and strlcat() only operโ€
     ate on true “C” strings.  This means that for strlcpy() src must be NUL-terminated and for
     strlcat() both src and dst must be NUL-terminated.

     The strlcpy() function copies up to size - 1 characters from the NUL-terminated string src to
     dst, NUL-terminating the result.

     The strlcat() function appends the NUL-terminated string src to the end of dst.  It will append
     at most size - strlen(dst) - 1 bytes, NUL-terminating the result.

: strlcpy() ๋ฐ strlcat() ๊ธฐ๋Šฅ์€ ๊ฐ๊ฐ ๋ฌธ์ž์—ด์„ ๋ณต์‚ฌํ•˜๊ณ  ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์€ strncpy(3) ๋ฐ strncat(3)์— ๋Œ€ํ•ด ๋” ์•ˆ์ „ํ•˜๊ณ  ์ผ๊ด€์„ฑ์ด ์žˆ์œผ๋ฉฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์‰ฌ์šด ๋Œ€์ฒด ๊ธฐ๋Šฅ์œผ๋กœ ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ๊ณผ ๋‹ฌ๋ฆฌ strlcpy() ๋ฐ strlcat()์€ ๋ฒ„ํผ์˜ ์ „์ฒด ํฌ๊ธฐ(๊ธธ์ด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ)๋ฅผ ๊ฐ€์ ธ์™€ NUL ์ข…๋ฃŒ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค(ํฌ๊ธฐ๊ฐ€ 0๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋˜๋Š” strlcat()์˜ ๊ฒฝ์šฐ dst์— ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ฐ”์ดํŠธ ํ”„๋ฆฌ๊ฐ€ ์žˆ์œผ๋ฉด). NUL์šฉ ๋ฐ”์ดํŠธ๋Š” ํฌ๊ธฐ์— ํฌํ•จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ strlcpy() ๋ฐ strlcat()์€ ์‹ค์ œ "C" ๋ฌธ์ž์—ด์—์„œ๋งŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” strlcpy()์˜ ๊ฒฝ์šฐ src๊ฐ€ NUL ์ข…๋ฃŒ๋˜์–ด์•ผ ํ•˜๋ฉฐ strlcat()์˜ ๊ฒฝ์šฐ src ๋ฐ dst ๋ชจ๋‘ NUL ์ข…๋ฃŒ๋˜์–ด์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
strlcpy() ํ•จ์ˆ˜๋Š” NUL ์ข…๋‹จ ๋ฌธ์ž์—ด src์—์„œ dst๋กœ ์ตœ๋Œ€ 1์ž ํฌ๊ธฐ๊นŒ์ง€ ๋ณต์‚ฌํ•˜์—ฌ NUL ์ข…๋‹จ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
strlcat() ํ•จ์ˆ˜๋Š” NUL ์ข…๋ฃŒ ๋ฌธ์ž์—ด src๋ฅผ dst์˜ ๋์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์€ ์ตœ๋Œ€ ํฌ๊ธฐ - strlen(dst) - 1 ๋ฐ”์ดํŠธ, NUL ์ข…๋ฃŒ ๊ฒฐ๊ณผ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. (ํ•ด์„๋ณธ)

 

RETURN VALUES
     The strlcpy() and strlcat() functions return the total length of the string they tried to creโ€
     ate.  For strlcpy() that means the length of src.  For strlcat() that means the initial length
     of dst plus the length of src.  While this may seem somewhat confusing, it was done to make
     truncation detection simple.

     Note, however, that if strlcat() traverses size characters without finding a NUL, the length of
     the string is considered to be size and the destination string will not be NUL-terminated (since
     there was no space for the NUL).  This keeps strlcat() from running off the end of a string.  In
     practice this should not happen (as it means that either size is incorrect or that dst is not a
     proper “C” string).  The check exists to prevent potential security problems in incorrect code.

: strlcpy()์™€ strlcat() ํ•จ์ˆ˜๋Š” ๊ทธ๋“ค์ด ๋งŒ๋“ค๋ ค๊ณ  ํ–ˆ๋˜ ๋ฌธ์ž์—ด์˜ ์ „์ฒด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. strlcpy()์˜ ๊ฒฝ์šฐ src์˜ ๊ธธ์ด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. dst์˜ ์ดˆ๊ธฐ ๊ธธ์ด์— src์˜ ๊ธธ์ด๋ฅผ ๋”ํ•œ ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š” strlcat()์˜ ๊ฒฝ์šฐ. ๋‹ค์†Œ ํ˜ผ๋ž€์Šค๋Ÿฌ์›Œ ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ์ ˆ๋‹จ ๊ฐ์ง€๋ฅผ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜ํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ strlcat()์ด NUL์„ ์ฐพ์ง€ ๋ชปํ•˜๊ณ  ํฌ๊ธฐ ๋ฌธ์ž๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” ํฌ๊ธฐ๋กœ ๊ฐ„์ฃผ๋˜๋ฉฐ ๋Œ€์ƒ ๋ฌธ์ž์—ด์€ NUL๋กœ ์ข…๋ฃŒ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค(NUL์„ ์œ„ํ•œ ๊ณต๊ฐ„์ด ์—†์—ˆ๊ธฐ ๋•Œ๋ฌธ). ์ด๋กœ ์ธํ•ด strlcat()์ด ๋ฌธ์ž์—ด ๋์—์„œ ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋Š” ์ด๋Ÿฐ ์ผ์ด ๋ฐœ์ƒํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค(ํฌ๊ธฐ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ๊ฑฐ๋‚˜ dst๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ "C" ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ). ์ด ๊ฒ€์‚ฌ๋Š” ์ž˜๋ชป๋œ ์ฝ”๋“œ์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ณด์•ˆ ๋ฌธ์ œ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. (ํ•ด์„๋ณธ)

 

๐Ÿ“Œ ์ž‘์„ฑ ์ฝ”๋“œ

size_t	do_strlcat(char *dst, const char *src, size_t size)
{
	size_t	dst_len;
	size_t	src_len;
	size_t	i;

	if (size == 0 && dst == NULL)
		return (do_strlen(src));
	src_len = do_strlen(src);
	dst_len = do_strlen(dst);
	i = 0;
	if (dst_len >= size)
		return (src_len + size);
	while (i + dst_len + 1 < size && src[i] != '\0')
	{
		dst[dst_len + i] = src[i];
		i ++;
	}
	dst[dst_len + i] = '\0';
	return (src_len + dst_len);
}

 

๐Ÿ“Œ ์ฝ”๋“œ ๋ฆฌ๋ทฐ

strlcat ์€ size๋งŒํผ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. dst ๋’ค์— src๋ฅผ ๋ถ™์—ฌ์ฃผ๋Š”๋ฐ, ์—ฌ๋Ÿฌ ์ƒํ™ฉ์ด ์กด์žฌํ•œ๋‹ค.

ํŠนํžˆ ๋ฐ˜ํ™˜๊ฐ’์€, ๋‘ ๋ฌธ์ž์—ด์ด ํ•ฉ์ณ์งˆ ๊ณต๊ฐ„์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ์ด ๋ฐ˜ํ™˜๊ฐ’์ด ํŠน์ดํ•˜๊ฒŒ ๋‚˜์˜จ๋‹ค.

 

1) size๊ฐ€ 0์ด๊ฑฐ๋‚˜ dst๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž์—ด์ด NULL์ธ ๊ฒฝ์šฐ, ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ  src์˜ ๊ธธ์ด๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

2) size ๋ณด๋‹ค dst์˜ ๊ธธ์ด๊ฐ€ ํฌ๊ฑฐ๋‚˜ ๊ฐ™์„ ๊ฒฝ์šฐ, ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ  src ๊ธธ์ด + size๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

3) size ๋ณด๋‹ค dst์˜ ๊ธธ์ด๊ฐ€ ์ž‘์€ ๊ฒฝ์šฐ, ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด src๋ฅผ dst์— ๋ถ™์ด๋Š” ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

4) src๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž์—ด์ด NULL์ธ ๊ฒฝ์šฐ, ๋ฐ˜๋ณต๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ , dst๋์— \0์„ ๋„ฃ๋Š” ๋™์ž‘๋งŒ ์ˆ˜ํ–‰ํ•œ๋‹ค.

(3, 4 ๋ฒˆ ์ƒํ™ฉ์—์„œ์˜ ๋ฐ˜ํ™˜๊ฐ’์€ src_len + dst_len ์ด๋‹ค.)

 

์šฐ์„  1๋ฒˆ ์ƒํ™ฉ์€ size๊ฐ€ 0์ด๋ผ์„œ ๋ถ™์ด๋Š” ๋™์ž‘์„ ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๊ณ , dst๊ฐ€ NULL์ด๋ฉด ๋ถ™์ผ ๊ณต๊ฐ„์ด ์ด๋ฏธ ๋ฌธ์ž์—ด์ด ์ข…๋ฃŒ๋˜์–ด

๋ถ™์ด๋Š” ๋™์ž‘์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ, ๋ฐ˜ํ™˜๊ฐ’์€ dst์˜ ๊ธธ์ด์™€ size๋ฅผ ์ œ์™ธํ•œ src์˜ ๊ธธ์ด๋งŒ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค.

2๋ฒˆ ์ƒํ™ฉ์€, dst๊ฐ€ size๋ณด๋‹ค ๋” ์ปค์„œ ๋ถ™์ด๋Š” ๋™์ž‘์„ ํ•  ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์ด๋‹ค. ๋”ฐ๋ผ์„œ size์˜ ๊ธธ์ด์™€ src ๊ธธ์ด๋งŒ ๋”ํ•ด์„œ

๋ฐ˜ํ™˜ํ•˜๋ฉด ํ•ฉ์ณ์งˆ ๊ณต๊ฐ„์˜ ๊ธธ์ด๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ•ฉ์ณ์ง€์ง€ ์•Š์ง€๋งŒ ํ•ฉ์ณ์กŒ์„ ๋•Œ์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ •ํ•œ๋‹ค.

3๋ฒˆ ์ƒํ™ฉ์€, ์ผ๋ฐ˜์ ์ธ ์ƒํ™ฉ์ธ๋ฐ, src๋ฅผ ๋ถ™์ด๋Š” ์ƒํ™ฉ์ด ์˜จ ๊ฒƒ์ด๊ณ , ์ด ๋•Œ๋Š” ์ด์ œ size๊ฐ€ ์•„๋‹Œ dst ๊ธธ์ด๋ฅผ ๋„ฃ์–ด์ค˜์•ผ

ํ•ฉ์ณ์งˆ ๊ณต๊ฐ„์˜ ํฌ๊ธฐ๊ฐ€ ๋งˆ๋ จ๋˜์–ด ๋ฐ˜ํ™˜ํ•˜๋ฉด ์›ํ•˜๋Š” ๊ธธ์ด๊ฐ€ ๋ฐ˜ํ™˜๋  ์ˆ˜ ์žˆ๋‹ค.

4๋ฒˆ ์ƒํ™ฉ์—์„œ๋Š” ๋‹จ์ˆœํžˆ 3๋ฒˆ ์ƒํ™ฉ์—์„œ src ๋‚ด์šฉ์ด ์—†๋Š” ๊ฒฝ์šฐ์ธ๋ฐ, dst๊ฐ€ ๊ทธ๋Œ€๋กœ ๋‚˜์˜ค๊ณ  ๋ฐ˜ํ™˜๋„ ๊ฒฐ๊ตญ dst๊ธธ์ด๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

 

๊ฒฐ๋ก ์€, src๊ฐ€ ํ•˜๋‚˜๋ผ๋„ dst์— ๋„ฃ์–ด์ง€๋Š” ์ƒํ™ฉ์ด ์˜จ๋‹ค๋ฉด, ๋ฐ˜ํ™˜๊ฐ’์ด src๊ธธ์ด + dst๊ธธ์ด๋กœ ๋ฐ”๋€๋‹ค.

์ด strlcat ํ•จ์ˆ˜๋Š” 'src์˜ ๊ธธ์ด๋ฅผ ํ•ญ์ƒ ํฌํ•จํ•œ ํ•ฉ์ณ์งˆ ๊ณต๊ฐ„์˜ ๊ฐ€๋Šฅํ•œ ๊ธธ์ด'๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.