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 // prepare output
u8* op; *s_outp = m_c8arrv(&op, oLen); u8* op; *s_outp = m_c8arrv(&op, oLen);
u8* ep; *s_errp = m_c8arrv(&ep, eLen); u8* ep; *s_errp = m_c8arrv(&ep, eLen);
if (oBuf!=NULL) { memcpy(op, oBuf, oLen*sizeof(char)); TFREE(oBuf); } if (oBuf!=NULL) { memcpy(op, oBuf, oLen*sizeof(char)); free(oBuf); }
if (eBuf!=NULL) { memcpy(ep, eBuf, eLen*sizeof(char)); TFREE(eBuf); } if (eBuf!=NULL) { memcpy(ep, eBuf, eLen*sizeof(char)); free(eBuf); }
return (i32)code; return (i32)code;
} }
#else #else

View File

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