下面是從GNU Coreutils的的lib/xreadlink.c文件中的一段代碼..如何檢查符號鏈接文件中的鏈接大小是否過大,是否適用於此代碼?
/* Call readlink to get the symbolic link value of FILENAME.
+ SIZE is a hint as to how long the link is expected to be;
+ typically it is taken from st_size. It need not be correct.
Return a pointer to that NUL-terminated string in malloc'd storage.
If readlink fails, return NULL (caller may use errno to diagnose).
If malloc fails, or if the link value is longer than SSIZE_MAX :-),
give a diagnostic and exit. */
char * xreadlink (char const *filename)
{
/* The initial buffer size for the link value. A power of 2
detects arithmetic overflow earlier, but is not required. */
size_t buf_size = 128;
while (1)
{
char* buffer = xmalloc(buf_size);
ssize_t link_length = readlink(filename, buffer, buf_size);
if(link_length < 0)
{
/*handle failure of system call*/
}
if((size_t) link_length < buf_size)
{
buffer[link_length] = 0;
return buffer;
}
/*size not sufficient, allocate more*/
free (buffer);
buf_size *= 2;
/*Check whether increase is possible*/
if (SSIZE_MAX < buf_size || (SIZE_MAX/2 < SSIZE_MAX && buf_size == 0))
xalloc_die();
}
}
的代碼是可以理解的,除了我不明白爲什麼在檢查鏈接的大小是過大的作品,即行:
if (SSIZE_MAX < buf_size || (SIZE_MAX/2 < SSIZE_MAX && buf_size == 0))
此外,
(SIZE_MAX/2 < SSIZE_MAX)
條件如何可以是真正的任何系統上???