Windows: sh: revert to using calloc() in reading threads

This commit is contained in:
vylsaz 2025-01-11 05:45:35 +00:00 committed by dzaima
parent 67c2850e38
commit 40609b2f5f
2 changed files with 20 additions and 16 deletions

View File

@ -1220,8 +1220,8 @@ static i32 sh_core(bool raw, B x, usz xia, B inObj, u64 iLen, B* s_outp, B* s_er
// prepare output
u8* op; *s_outp = m_c8arrv(&op, oLen);
u8* ep; *s_errp = m_c8arrv(&ep, eLen);
if (oBuf!=NULL) { memcpy(op, oBuf, oLen*sizeof(char)); TFREE(oBuf); }
if (eBuf!=NULL) { memcpy(ep, eBuf, eLen*sizeof(char)); TFREE(eBuf); }
if (oBuf!=NULL) { memcpy(op, oBuf, oLen*sizeof(char)); free(oBuf); }
if (eBuf!=NULL) { memcpy(ep, eBuf, eLen*sizeof(char)); free(eBuf); }
return (i32)code;
}
#else

View File

@ -1,5 +1,4 @@
#include <windows.h>
#include "../utils/talloc.h"
typedef struct {
HANDLE hndl;
@ -29,28 +28,33 @@ static DWORD WINAPI winThreadRead(LPVOID arg0) {
DWORD dwResult = ERROR_SUCCESS;
ThreadIO* arg = arg0;
HANDLE hndl = arg->hndl;
u8 buf[1024] = {0};
u8 buf[4096] = {0};
const usz bufSize = sizeof(buf)/sizeof(u8);
DWORD dwRead = 0;
TSALLOC(char, rBuf, 8);
DWORD dwRead = 0, dwHasRead = 0;
char* rBuf = NULL;
for (;;) {
ZeroMemory(buf, bufSize);
BOOL bOk = ReadFile(hndl, buf, bufSize, &dwRead, NULL);
if (dwRead == 0) { break; }
if (!bOk) {
DWORD dwErr = GetLastError();
if (dwErr == ERROR_BROKEN_PIPE) { break; }
else { dwResult = dwErr; break; }
dwResult = GetLastError();
break;
}
TSADDA(rBuf, buf, dwRead);
char* newBuf = (rBuf == NULL)?
calloc(dwHasRead+dwRead, sizeof(char)) :
realloc(rBuf, (dwHasRead+dwRead)*sizeof(char));
if (newBuf == NULL) { dwResult = GetLastError(); break; }
rBuf = newBuf;
memcpy(&rBuf[dwHasRead], buf, dwRead);
dwHasRead += dwRead;
}
if (dwResult == ERROR_SUCCESS) {
arg->len = TSSIZE(rBuf);
arg->buf = TALLOCP(char, arg->len);
memcpy(arg->buf, rBuf, arg->len);
if (dwResult != ERROR_SUCCESS) {
if (rBuf != NULL) { free(rBuf); }
} else {
arg->buf = rBuf;
arg->len = dwHasRead;
}
TSFREE(rBuf);
CloseHandle(hndl);
return dwResult;
}