Skip to content

Commit

Permalink
utils: Use more RB_ prefixed ALLOCV macros
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Jul 29, 2024
1 parent eef4e49 commit 5dc9f7e
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions ext/winevt/winevt_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ wstr_to_rb_str(UINT cp, const WCHAR* wstr, int clen)
}

int len = WideCharToMultiByte(cp, 0, wstr, clen, nullptr, 0, nullptr, nullptr);
ptr = ALLOCV_N(CHAR, vstr, len);
ptr = RB_ALLOCV_N(CHAR, vstr, len);
// For memory safety.
ZeroMemory(ptr, sizeof(CHAR) * len);
ret = WideCharToMultiByte(cp, 0, wstr, clen, ptr, len, nullptr, nullptr);
// return 0 should be failure.
// ref: https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte#return-value
if (ret == 0) {
err = GetLastError();
ALLOCV_END(vstr);
RB_ALLOCV_END(vstr);
raise_system_error(rb_eRuntimeError, err);
}
VALUE str = rb_utf8_str_new_cstr(ptr);
ALLOCV_END(vstr);
RB_ALLOCV_END(vstr);

return str;
}
Expand Down Expand Up @@ -85,18 +85,18 @@ render_to_rb_str(EVT_HANDLE handle, DWORD flags)
EvtRender(nullptr, handle, flags, 0, NULL, &bufferSize, &count);

// bufferSize is in bytes, not characters
buffer = (WCHAR*)ALLOCV(vbuffer, bufferSize);
buffer = (WCHAR*)RB_ALLOCV(vbuffer, bufferSize);

succeeded =
EvtRender(nullptr, handle, flags, bufferSize, buffer, &bufferSizeUsed, &count);
if (!succeeded) {
DWORD status = GetLastError();
ALLOCV_END(vbuffer);
RB_ALLOCV_END(vbuffer);
raise_system_error(rb_eWinevtQueryError, status);
}

result = wstr_to_rb_str(CP_UTF8, buffer, -1);
ALLOCV_END(vbuffer);
RB_ALLOCV_END(vbuffer);

return result;
}
Expand Down Expand Up @@ -153,7 +153,7 @@ make_displayable_binary_string(PBYTE bin, size_t length)
return rb_str_new2("(NULL)");
}

buffer = ALLOCV_N(CHAR, vbuffer, size);
buffer = RB_ALLOCV_N(CHAR, vbuffer, size);

for (i = 0; i < length; i++) {
for (j = 0; j < 2; j++) {
Expand All @@ -164,7 +164,7 @@ make_displayable_binary_string(PBYTE bin, size_t length)
buffer[size - 1] = '\0';

VALUE str = rb_str_new2(buffer);
ALLOCV_END(vbuffer);
RB_ALLOCV_END(vbuffer);

return str;
}
Expand Down Expand Up @@ -380,7 +380,7 @@ get_values(EVT_HANDLE handle)
renderContext, handle, EvtRenderEventValues, 0, NULL, &bufferSize, &propCount);

// bufferSize is in bytes, not array size
pRenderedValues = (PEVT_VARIANT)ALLOCV(vbuffer, bufferSize);
pRenderedValues = (PEVT_VARIANT)RB_ALLOCV(vbuffer, bufferSize);

succeeded = EvtRender(renderContext,
handle,
Expand All @@ -391,14 +391,14 @@ get_values(EVT_HANDLE handle)
&propCount);
if (!succeeded) {
DWORD status = GetLastError();
ALLOCV_END(vbuffer);
RB_ALLOCV_END(vbuffer);
EvtClose(renderContext);
raise_system_error(rb_eWinevtQueryError, status);
}

userValues = extract_user_evt_variants(pRenderedValues, propCount);

ALLOCV_END(vbuffer);
RB_ALLOCV_END(vbuffer);
EvtClose(renderContext);

return userValues;
Expand Down Expand Up @@ -608,7 +608,7 @@ static char* convert_wstr(wchar_t *wstr)
return NULL;
}

ptr = ALLOCV_N(CHAR, vstr, len);
ptr = RB_ALLOCV_N(CHAR, vstr, len);
// For memory safety.
ZeroMemory(ptr, sizeof(CHAR) * len);

Expand All @@ -617,7 +617,7 @@ static char* convert_wstr(wchar_t *wstr)
// ref: https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte#return-value
if (len == 0) {
err = GetLastError();
ALLOCV_END(vstr);
RB_ALLOCV_END(vstr);
raise_system_error(rb_eRuntimeError, err);
}

Expand Down Expand Up @@ -687,7 +687,7 @@ static int ExpandSIDWString(PSID sid, CHAR **out_expanded)
error:
err = GetLastError();

ALLOCV_END(vformatted);
RB_ALLOCV_END(vformatted);
if (domain != NULL) {
free(domain);
}
Expand Down Expand Up @@ -734,7 +734,7 @@ render_system_event(EVT_HANDLE hEvent, BOOL preserve_qualifiers, BOOL preserveSI
status = GetLastError();
if (ERROR_INSUFFICIENT_BUFFER == status) {
dwBufferSize = dwBufferUsed;
pRenderedValues = (PEVT_VARIANT)ALLOCV(vRenderedValues, dwBufferSize);
pRenderedValues = (PEVT_VARIANT)RB_ALLOCV(vRenderedValues, dwBufferSize);
if (pRenderedValues) {
EvtRender(hContext,
hEvent,
Expand All @@ -752,7 +752,7 @@ render_system_event(EVT_HANDLE hEvent, BOOL preserve_qualifiers, BOOL preserveSI

if (ERROR_SUCCESS != status) {
EvtClose(hContext);
ALLOCV_END(vRenderedValues);
RB_ALLOCV_END(vRenderedValues);

rb_raise(rb_eWinevtQueryError, "EvtRender failed with %lu\n", status);
}
Expand Down Expand Up @@ -904,7 +904,7 @@ render_system_event(EVT_HANDLE hEvent, BOOL preserve_qualifiers, BOOL preserveSI
}

EvtClose(hContext);
ALLOCV_END(vRenderedValues);
RB_ALLOCV_END(vRenderedValues);

return hash;
}

0 comments on commit 5dc9f7e

Please sign in to comment.