From 4acc1960e69fd5e86198e479088e7be50459337b Mon Sep 17 00:00:00 2001 From: Oleg Oshmyan Date: Sat, 26 Jun 2021 15:21:55 +0300 Subject: directwrite: remove arbitrary 256-char name length limit --- libass/ass_directwrite.c | 38 ++++++++++++++++++++++++++++---------- libass/dwrite_c.h | 5 ++++- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/libass/ass_directwrite.c b/libass/ass_directwrite.c index 89e1099..deabb57 100644 --- a/libass/ass_directwrite.c +++ b/libass/ass_directwrite.c @@ -29,7 +29,6 @@ #include "ass_directwrite.h" #include "ass_utils.h" -#define NAME_MAX_LENGTH 256 #define FALLBACK_DEFAULT_FONT L"Arial" static const ASS_FontMapping font_substitutions[] = { @@ -428,19 +427,38 @@ static int encode_utf16(wchar_t *chars, uint32_t codepoint) static char *get_utf8_name(IDWriteLocalizedStrings *names, int k) { - wchar_t temp_name[NAME_MAX_LENGTH]; - HRESULT hr = IDWriteLocalizedStrings_GetString(names, k, - temp_name, - NAME_MAX_LENGTH); + wchar_t *temp_name = NULL; + char *mbName = NULL; + + UINT32 length; + HRESULT hr = IDWriteLocalizedStrings_GetStringLength(names, k, &length); if (FAILED(hr)) - return NULL; + goto cleanup; + + if (length >= (UINT32) -1 || length + (size_t) 1 > SIZE_MAX / sizeof(wchar_t)) + goto cleanup; - temp_name[NAME_MAX_LENGTH-1] = 0; - int size_needed = WideCharToMultiByte(CP_UTF8, 0, temp_name, -1, NULL, 0, NULL, NULL); - char *mbName = (char *) malloc(size_needed); + temp_name = (wchar_t *) malloc((length + 1) * sizeof(wchar_t)); + if (!temp_name) + goto cleanup; + + hr = IDWriteLocalizedStrings_GetString(names, k, temp_name, length + 1); + if (FAILED(hr)) + goto cleanup; + + int size_needed = + WideCharToMultiByte(CP_UTF8, 0, temp_name, -1, NULL, 0, NULL, NULL); + if (!size_needed) + goto cleanup; + + mbName = (char *) malloc(size_needed); if (!mbName) - return NULL; + goto cleanup; + WideCharToMultiByte(CP_UTF8, 0, temp_name, -1, mbName, size_needed, NULL, NULL); + +cleanup: + free(temp_name); return mbName; } diff --git a/libass/dwrite_c.h b/libass/dwrite_c.h index 5612e9f..43930ca 100644 --- a/libass/dwrite_c.h +++ b/libass/dwrite_c.h @@ -506,8 +506,10 @@ DECLARE_INTERFACE_(IDWriteLocalizedStrings,IUnknown) STDMETHOD(dummy1)(THIS); STDMETHOD(dummy2)(THIS); STDMETHOD(dummy3)(THIS); - STDMETHOD(dummy4)(THIS); + STDMETHOD(GetStringLength)(THIS_ + UINT32 index, + UINT32 *length) PURE; STDMETHOD(GetString)(THIS_ UINT32 index, WCHAR *stringBuffer, @@ -518,6 +520,7 @@ DECLARE_INTERFACE_(IDWriteLocalizedStrings,IUnknown) #ifdef COBJMACROS #define IDWriteLocalizedStrings_Release(This) (This)->lpVtbl->Release(This) #define IDWriteLocalizedStrings_GetCount(This) (This)->lpVtbl->GetCount(This) +#define IDWriteLocalizedStrings_GetStringLength(This,index,length) (This)->lpVtbl->GetStringLength(This,index,length) #define IDWriteLocalizedStrings_GetString(This,index,stringBuffer,size) (This)->lpVtbl->GetString(This,index,stringBuffer,size) #endif /*COBJMACROS*/ -- cgit v1.2.3