Skip to content

Commit

Permalink
More changes how we use WideCharToMutliByte
Browse files Browse the repository at this point in the history
Handle case where there is no data pushed in.
  • Loading branch information
bakpakin committed Aug 18, 2024
1 parent e53d22f commit 33d2f9a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/core/filewatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,17 @@ typedef struct {
uint64_t buf[FILE_INFO_PADDING / sizeof(uint64_t)]; /* Ensure alignment */
} OverlappedWatch;

#define NotifyChange FILE_NOTIFY_INFORMATION

static void read_dir_changes(OverlappedWatch *ow) {
BOOL result = ReadDirectoryChangesExW(ow->stream->handle,
(FILE_NOTIFY_EXTENDED_INFORMATION *) ow->buf,
BOOL result = ReadDirectoryChangesW(ow->stream->handle,
(NotifyChange *) ow->buf,
FILE_INFO_PADDING,
(ow->flags & WATCHFLAG_RECURSIVE) ? TRUE : FALSE,
ow->flags & ~WATCHFLAG_RECURSIVE,
NULL,
(OVERLAPPED *) ow,
NULL,
ReadDirectoryNotifyExtendedInformation);
NULL);
if (!result) {
janet_panicv(janet_ev_lasterr());
}
Expand Down Expand Up @@ -373,27 +374,34 @@ static void watcher_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
break;
}

FILE_NOTIFY_EXTENDED_INFORMATION *fni = (FILE_NOTIFY_EXTENDED_INFORMATION *) ow->buf;
NotifyChange *fni = (NotifyChange *) ow->buf;

while (1) {
/* Got an event */

/* Extract name */
uint8_t tempbuf[FILE_INFO_PADDING];
int32_t nbytes = (int32_t) WideCharToMultiByte(CP_UTF8, WC_SEPCHARS, fni->FileName, fni->FileNameLength / 2, tempbuf, sizeof(tempbuf), NULL, NULL);
JanetString filename = janet_string(tempbuf, nbytes);
Janet filename;
if (fni->FileNameLength) {
int32_t nbytes = (int32_t) WideCharToMultiByte(CP_UTF8, 0, fni->FileName, fni->FileNameLength / sizeof(wchar_t), NULL, 0, NULL, NULL);
janet_assert(nbytes, "bad utf8 path");
uint8_t *into = janet_string_begin(nbytes);
WideCharToMultiByte(CP_UTF8, 0, fni->FileName, fni->FileNameLength / sizeof(wchar_t), into, nbytes, NULL, NULL);
filename = janet_wrap_string(janet_string_end(into));
} else {
filename = janet_cstringv("");
}

JanetKV *event = janet_struct_begin(3);
janet_struct_put(event, janet_ckeywordv("action"), janet_ckeywordv(watcher_actions_windows[fni->Action]));
janet_struct_put(event, janet_ckeywordv("file-name"), janet_wrap_string(filename));
janet_struct_put(event, janet_ckeywordv("file-name"), filename);
janet_struct_put(event, janet_ckeywordv("dir"), janet_wrap_string(ow->dir_path));
Janet eventv = janet_wrap_struct(janet_struct_end(event));

janet_channel_give(watcher->channel, eventv);

/* Next event */
if (!fni->NextEntryOffset) break;
fni = (FILE_NOTIFY_EXTENDED_INFORMATION *) ((char *)fni + fni->NextEntryOffset);
fni = (NotifyChange *) ((char *)fni + fni->NextEntryOffset);
}

/* Make another call to read directory changes */
Expand Down
2 changes: 1 addition & 1 deletion tools/afl/parser_testcases/simple.janet
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ true
@[1 "hello"]
nil
(foo 2 3)
([{} @{:k ([""])}])
([{} @{:k ([""])}])

0 comments on commit 33d2f9a

Please sign in to comment.