Skip to content

Commit a339f00

Browse files
committed
Windows: fix potential memory curruption bug in unicode.c
Also add ffStrbufInitWS
1 parent de15866 commit a339f00

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Bugfixes:
1010
* Fix iTerm being detected as iTermServer-* sometimes
1111
* Fix sound device volume being incorrectly detected as muted sometimes (Sound)
1212
* Fix memleaks reported by LeakSanitizer (Linux)
13+
* Fix potential memory curruption bug in unicode.c (Windows)
1314

1415
Logo:
1516
* Update Windows 11 ASCII logo to look more visually consistent (#445)

src/util/windows/unicode.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,17 @@ void ffStrbufSetNWS(FFstrbuf* result, uint32_t length, const wchar_t* source)
1717
result->chars[size_needed] = '\0';
1818
}
1919

20-
FFstrbuf ffStrbufCreateNWS(uint32_t length, const wchar_t* source)
20+
void ffStrbufInitNWS(FFstrbuf* result, uint32_t length, const wchar_t* source)
2121
{
22-
FFstrbuf result;
23-
24-
if(length == 0)
25-
ffStrbufInit(&result);
26-
else
22+
if(!length)
2723
{
28-
int size_needed = WideCharToMultiByte(CP_UTF8, 0, source, (int)length, NULL, 0, NULL, NULL);
29-
ffStrbufInitA(&result, (uint32_t)size_needed);
30-
WideCharToMultiByte(CP_UTF8, 0, source, (int)length, result.chars, size_needed, NULL, NULL);
31-
result.length = (uint32_t)size_needed;
32-
result.chars[size_needed] = '\0';
24+
ffStrbufInit(result);
25+
return;
3326
}
3427

35-
return result;
28+
int size_needed = WideCharToMultiByte(CP_UTF8, 0, source, (int)length, NULL, 0, NULL, NULL);
29+
ffStrbufInitA(result, (uint32_t)size_needed + 1);
30+
WideCharToMultiByte(CP_UTF8, 0, source, (int)length, result->chars, size_needed, NULL, NULL);
31+
result->length = (uint32_t)size_needed;
32+
result->chars[size_needed] = '\0';
3633
}

src/util/windows/unicode.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@ static inline void ffStrbufSetWS(FFstrbuf* result, const wchar_t* source)
1313
return ffStrbufSetNWS(result, (uint32_t)wcslen(source), source);
1414
}
1515

16-
FFstrbuf ffStrbufCreateNWS(uint32_t length, const wchar_t* source);
16+
void ffStrbufInitNWS(FFstrbuf* result, uint32_t length, const wchar_t* source);
17+
18+
static inline void ffStrbufInitWS(FFstrbuf* result, const wchar_t* source)
19+
{
20+
return ffStrbufInitNWS(result, (uint32_t)wcslen(source), source);
21+
}
22+
23+
static inline FFstrbuf ffStrbufCreateNWS(uint32_t length, const wchar_t* source)
24+
{
25+
FFstrbuf result;
26+
ffStrbufInitNWS(&result, length, source);
27+
return result;
28+
}
1729

1830
static inline FFstrbuf ffStrbufCreateWS(const wchar_t* source)
1931
{

0 commit comments

Comments
 (0)