开发者

getcwd second parameter

开发者 https://www.devze.com 2023-02-11 10:19 出处:网络
What should i fill in the second parameter of the function getcwd if I am reading开发者_运维百科 the current directory?The size of the buffer you want to fill:

What should i fill in the second parameter of the function getcwd if I am reading开发者_运维百科 the current directory?


The size of the buffer you want to fill:

char result[PATH_MAX];
char *r = getcwd(result, PATH_MAX);

Failure to set this correctly (or spot ENAMETOOLONG/ERANGE) could lead to buffer overflow problems.

Caveat: Not all platforms provide PATH_MAX. If you can be sure it's there on your platforms it is quite handy.

You can also use realpath(), (POSIX.1-2008) which will malloc() memory for you to do this more cleanly:

char *result = realpath(".", NULL);
// do stuff with result
free(result);


The length of the buffer you provide in the first parameter, so that overflow cannot occur.

0

精彩评论

暂无评论...
验证码 换一张
取 消