Skip to content

Commit f86808d

Browse files
committed
posix: getcwd now expects buf not null
1 parent 1ffa8c4 commit f86808d

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

src/posix/unistd.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ int chdir(const char *path)
229229

230230
char *getcwd(char *buf, size_t size)
231231
{
232-
assert(cwd.front() == '/');
232+
Expects(cwd.front() == '/');
233+
Expects(buf != nullptr);
234+
233235
if (size == 0)
234236
{
235237
errno = EINVAL;

test/posix/integration/stat/stat_tests.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,33 @@ void stat_tests()
142142
}
143143
CHECKSERT(res == 0, "chdir to subfolder of cwd is ok");
144144

145+
/**
146+
If buf is a null pointer, the behavior of getcwd() is unspecified.
147+
http://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html
148+
149+
Changed behavior of getcwd to Expect buf isn't nullptr.
150+
151+
TODO: It's nice to have these test cases in there, but it will require
152+
the test to throw on contract violation
153+
**/
154+
155+
/**
145156
char* nullcwd = getcwd(nullbuf, 0);
146157
printf("getcwd result (nullptr, size 0): %s\n", nullcwd == nullptr ? "NULL" : nullcwd);
147158
if (nullcwd == nullptr)
148-
{
149-
printf("getcwd error: %s\n", strerror(errno));
150-
}
159+
{
160+
printf("getcwd error: %s\n", strerror(errno));
161+
}
151162
CHECKSERT(nullcwd == nullptr && errno == EINVAL, "getcwd() with 0-size buffer should fail with EINVAL");
152163
153164
nullcwd = getcwd(nullptr, 1024);
154165
printf("getcwd result (nullptr): %s\n", nullcwd == nullptr ? "NULL" : nullcwd);
155166
if (nullcwd == nullptr)
156-
{
157-
printf("getcwd error: %s\n", strerror(errno));
158-
}
167+
{
168+
printf("getcwd error: %s\n", strerror(errno));
169+
}
159170
CHECKSERT(nullcwd == nullptr, "getcwd() with nullptr buffer should fail");
171+
**/
160172

161173
char* shortcwd = getcwd(shortbuf, 4);
162174
printf("getcwd result (small buffer): %s\n", shortcwd == nullptr ? "NULL" : shortcwd);

test/posix/integration/stat/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def increment(line):
2525
print "num_outputs after increment: ", num_outputs
2626

2727
def check_num_outputs(line):
28-
assert(num_outputs == 20)
29-
vmrunner.vms[0].exit(0, "SUCCESS")
28+
assert(num_outputs == 18)
29+
vmrunner.vms[0].exit(0, "All tests passed", keep_running = True)
3030

3131
vm.on_output("stat\(\) with nullptr buffer fails with EFAULT", increment)
3232
vm.on_output("stat\(\) of folder that exists is ok", increment)

0 commit comments

Comments
 (0)