Skip to content

Commit 542bdea

Browse files
committed
Fix resolving current working directory
Define `WREN_PATH_MAX` recursively using `PATH_MAX` or use fallback. Handle `UV_ENOBUFS` error.
1 parent b82cf5a commit 542bdea

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/module/os.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,18 @@ void processCwd(WrenVM* vm)
8181
{
8282
wrenEnsureSlots(vm, 1);
8383

84-
char buffer[WREN_PATH_MAX * 4];
85-
size_t length = sizeof(buffer);
86-
if (uv_cwd(buffer, &length) != 0)
84+
char _buffer[PATH_MAX * 2 + 1];
85+
char* buffer = _buffer;
86+
size_t length = sizeof(_buffer);
87+
int result = uv_cwd(buffer, &length);
88+
89+
if (result == UV_ENOBUFS)
90+
{
91+
buffer = (char *)malloc(length);
92+
result = uv_cwd(buffer, &length);
93+
}
94+
95+
if (result != 0)
8796
{
8897
wrenSetSlotString(vm, 0, "Cannot get current working directory.");
8998
wrenAbortFiber(vm, 0);

src/module/os.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#ifndef process_h
22
#define process_h
33

4+
#include "limits.h"
45
#include "wren.h"
56

6-
#define WREN_PATH_MAX 4096
7+
#ifdef PATH_MAX
8+
# define WREN_PATH_MAX PATH_MAX
9+
#else
10+
# define WREN_PATH_MAX 4096
11+
#endif
712

813
// Stores the command line arguments passed to the CLI.
914
void osSetArguments(int argc, const char* argv[]);

0 commit comments

Comments
 (0)