From 3eb0fab3173c257994e80bd9cca741ec02498503 Mon Sep 17 00:00:00 2001 From: nhmall Date: Wed, 27 Dec 2023 14:56:03 -0500 Subject: [PATCH] Windows error checking and warnings --- sys/windows/vs/NetHackW/NetHackW.vcxproj | 21 ++- sys/windows/windsys.c | 66 +++++---- win/win32/NetHackW.c | 4 + win/win32/mhdlg.c | 1 + win/win32/mhmain.c | 148 +++++++++++--------- win/win32/mhmenu.c | 168 +++++++++++++---------- win/win32/mhmsgwnd.c | 1 + win/win32/mhrip.c | 11 +- win/win32/mhtext.c | 11 +- win/win32/mswproc.c | 59 ++++---- 10 files changed, 286 insertions(+), 204 deletions(-) diff --git a/sys/windows/vs/NetHackW/NetHackW.vcxproj b/sys/windows/vs/NetHackW/NetHackW.vcxproj index 802d37edd..eaff925d8 100644 --- a/sys/windows/vs/NetHackW/NetHackW.vcxproj +++ b/sys/windows/vs/NetHackW/NetHackW.vcxproj @@ -42,6 +42,9 @@ false + + true + /Gs /Oi- /w44774 %(AdditionalOptions) @@ -49,7 +52,7 @@ true $(WinWin32Dir);$(IncDir);$(SysWindDir);$(SysShareDir);$(WinShareDir);$(LuaDir);%(AdditionalIncludeDirectories) TILES;_WINDOWS;DLB;MSWIN_GRAPHICS;SAFEPROCS;NOTTYGRAPHICS;SND_LIB_WINDSOUND;USER_SOUNDS;HAS_STDINT_H;PDC_WIDE;%(PreprocessorDefinitions) - 4820;4706;4244;4245;4100;4310 + 4820;4706;4244;4245;4100;4310;6001 4820;4706;4244;4245;4100;4310 @@ -73,7 +76,7 @@ 4701;4702;4244;4310;4774 %(AdditionalOptions) /wd4774 - 4701;4702;4244;4310;4774;4324 + 4701;4702;4244;4310;4774;4324;6011;6297;6305;6385;6386;6387;28182 4701;4702;4244;4310;4774;4324 @@ -147,7 +150,9 @@ - + + 4820;4706;4244;4245;4100;4310;6001 + @@ -177,7 +182,9 @@ - + + 4820;4706;4244;4245;4100;4310;6001 + @@ -200,7 +207,9 @@ - + + 4820;4706;4244;4245;4100;4310;6001 + @@ -351,4 +360,4 @@ - + \ No newline at end of file diff --git a/sys/windows/windsys.c b/sys/windows/windsys.c index 8a970aa93..89061bc38 100644 --- a/sys/windows/windsys.c +++ b/sys/windows/windsys.c @@ -161,7 +161,7 @@ chdrive(char* str) char drive; if ((ptr = strchr(str, ':')) != (char *) 0) { drive = toupper((uchar) *(ptr - 1)); - _chdrive((drive - 'A') + 1); + (void) _chdrive((drive - 'A') + 1); } } @@ -388,18 +388,21 @@ void port_insert_pastebuf(char *buf) /* Housekeeping need: +GlobalUnlock(hglbCopy), GlobalFree(hglbCopy), CloseClipboard(), free(w) */ - memcpy(lpwstrCopy, w, abytes); - GlobalUnlock(hglbCopy); - /* Housekeeping need: GlobalFree(hglbCopy), CloseClipboard(), free(w) */ + if (lpwstrCopy) { + memcpy(lpwstrCopy, w, abytes); + GlobalUnlock(hglbCopy); + /* Housekeeping need: GlobalFree(hglbCopy), CloseClipboard(), free(w) + */ - /* put it on the clipboard */ - hresult = SetClipboardData(CF_UNICODETEXT, hglbCopy); - if (!hresult) { - raw_printf("Error copying to clipboard.\n"); - GlobalFree(hglbCopy); /* only needed if clipboard didn't accept data */ + /* put it on the clipboard */ + hresult = SetClipboardData(CF_UNICODETEXT, hglbCopy); + if (!hresult) { + raw_printf("Error copying to clipboard.\n"); + GlobalFree( + hglbCopy); /* only needed if clipboard didn't accept data */ + } + /* Housekeeping need: CloseClipboard(), free(w) */ } - /* Housekeeping need: CloseClipboard(), free(w) */ - CloseClipboard(); free(w); return; @@ -619,30 +622,33 @@ BOOL winos_font_support_cp437(HFONT hFont) HDC hdc = GetDC(NULL); HFONT oldFont = SelectObject(hdc, hFont); - DWORD size = GetFontUnicodeRanges(hdc, NULL); - GLYPHSET *glyphSet = (GLYPHSET *) malloc(size); - if (glyphSet != NULL) { - GetFontUnicodeRanges(hdc, glyphSet); + DWORD size = (size_t) GetFontUnicodeRanges(hdc, NULL); + if (size) { + GLYPHSET *glyphSet = (GLYPHSET *) malloc((size_t) size); + if (glyphSet != NULL) { +#pragma warning( push ) +#pragma warning( disable : 6386 ) + size = GetFontUnicodeRanges(hdc, glyphSet); +#pragma warning( pop ) + allFound = TRUE; + for (int i = 0; i < 256 && allFound; i++) { + WCHAR wc = cp437[i]; + BOOL found = FALSE; + for (DWORD j = 0; j < glyphSet->cRanges && !found; j++) { + WCHAR first = glyphSet->ranges[j].wcLow; + WCHAR last = first + glyphSet->ranges[j].cGlyphs - 1; - allFound = TRUE; - for (int i = 0; i < 256 && allFound; i++) { - WCHAR wc = cp437[i]; - BOOL found = FALSE; - for (DWORD j = 0; j < glyphSet->cRanges && !found; j++) { - WCHAR first = glyphSet->ranges[j].wcLow; - WCHAR last = first + glyphSet->ranges[j].cGlyphs - 1; - - if (wc >= first && wc <= last) - found = TRUE; + if (wc >= first && wc <= last) + found = TRUE; + } + if (!found) + allFound = FALSE; } - if (!found) - allFound = FALSE; + + free(glyphSet); } - - free(glyphSet); } - SelectObject(hdc, oldFont); ReleaseDC(NULL, hdc); diff --git a/win/win32/NetHackW.c b/win/win32/NetHackW.c index 5a1e7fd15..e418eb829 100644 --- a/win/win32/NetHackW.c +++ b/win/win32/NetHackW.c @@ -77,6 +77,8 @@ static void __cdecl mswin_moveloop(void *); #define MAX_CMDLINE_PARAM 255 +#pragma warning( push ) +#pragma warning( disable : 28251) int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) @@ -246,6 +248,8 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, return 0; } +#pragma warning( pop ) + PNHWinApp GetNHApp(void) { diff --git a/win/win32/mhdlg.c b/win/win32/mhdlg.c index 76874634b..6246b4362 100644 --- a/win/win32/mhdlg.c +++ b/win/win32/mhdlg.c @@ -136,6 +136,7 @@ GetlinDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_COMMAND: { TCHAR wbuf2[BUFSZ]; + wbuf2[BUFSZ - 1] = '\0'; switch (LOWORD(wParam)) { /* OK button was pressed */ case IDOK: diff --git a/win/win32/mhmain.c b/win/win32/mhmain.c index fb91e8f0d..3d405e106 100644 --- a/win/win32/mhmain.c +++ b/win/win32/mhmain.c @@ -190,7 +190,7 @@ static const char scanmap[] = { 'b', 'n', 'm', ',', '.', '?' /* ... */ }; -#define IDT_FUZZ_TIMER 100 +#define IDT_FUZZ_TIMER 100 /* // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) @@ -383,7 +383,7 @@ MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) c = 0; ZeroMemory(kbd_state, sizeof(kbd_state)); - GetKeyboardState(kbd_state); + (void) GetKeyboardState(kbd_state); if (ToAscii((UINT) wParam, (lParam >> 16) & 0xFF, kbd_state, &c, 0)) { NHEVENT_KBD(c & 0xFF); @@ -922,16 +922,23 @@ onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) #ifdef ENHANCED_SYMBOLS if (SYMHANDLING(H_UTF8)) { - WCHAR *p_copy = (WCHAR *) GlobalLock(hglbCopy); - wcsncpy(p_copy, wp, len); - p_copy[len] = 0; // null character - } else + WCHAR *p_copy; + + if ((p_copy = (WCHAR *) GlobalLock(hglbCopy)) != 0) { + wcsncpy(p_copy, wp, len); + p_copy[len] = 0; // null character + } + } else { #endif - { - char *p_copy = (char *) GlobalLock(hglbCopy); - strncpy(p_copy, p, len); - p_copy[len] = 0; // null character + char *p_copy; + + if ((p_copy = (char *) GlobalLock(hglbCopy)) != 0) { + strncpy(p_copy, p, len); + p_copy[len] = 0; // null character + } +#ifdef ENHANCED_SYMBOLS } +#endif GlobalUnlock(hglbCopy); #ifdef ENHANCED_SYMBOLS @@ -1001,28 +1008,33 @@ onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) text = nh_compose_ascii_screenshot(); if (!text) return FALSE; - tlen = strlen(text); wtext = (wchar_t *) malloc(tlen * sizeof(wchar_t)); - if (!wtext) - panic("out of memory"); - MultiByteToWideChar(NH_CODEPAGE, 0, text, -1, wtext, tlen); + if (wtext) { + MultiByteToWideChar(NH_CODEPAGE, 0, text, -1, wtext, tlen); + } } + filename[SIZE(filename) - 1] = '\0'; pFile = _tfopen(filename, TEXT("wt+,ccs=UTF-8")); if (!pFile) { TCHAR buf[4096]; _stprintf(buf, TEXT("Cannot open %s for writing!"), filename); NHMessageBox(hWnd, buf, MB_OK | MB_ICONERROR); - free(text); - free(wtext); + if (text) + free(text); + if (wtext) + free(wtext); return FALSE; } - fwrite(wtext, tlen * sizeof(wchar_t), 1, pFile); - fclose(pFile); - free(text); - free(wtext); + if (wtext) { + fwrite(wtext, tlen * sizeof(wchar_t), 1, pFile); + fclose(pFile); + free(wtext); + } + if (text) + free(text); } break; case IDM_NHMODE: { @@ -1307,26 +1319,28 @@ nh_compose_ascii_screenshot(void) text = (PMSNHMsgGetText) malloc(sizeof(MSNHMsgGetText) + TEXT_BUFFER_SIZE); - text->max_size = - TEXT_BUFFER_SIZE - - 1; /* make sure we always have 0 at the end of the buffer */ + if (text && retval) { + text->max_size = + TEXT_BUFFER_SIZE + - 1; /* make sure we always have 0 at the end of the buffer */ - ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); - SendMessage(mswin_hwnd_from_winid(WIN_MESSAGE), WM_MSNH_COMMAND, - (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); - strcpy(retval, text->buffer); + ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); + SendMessage(mswin_hwnd_from_winid(WIN_MESSAGE), WM_MSNH_COMMAND, + (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); + strcpy(retval, text->buffer); - ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); - SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_MSNH_COMMAND, - (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); - strcat(retval, text->buffer); + ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); + SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_MSNH_COMMAND, + (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); + strcat(retval, text->buffer); - ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); - SendMessage(mswin_hwnd_from_winid(WIN_STATUS), WM_MSNH_COMMAND, - (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); - strcat(retval, text->buffer); + ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); + SendMessage(mswin_hwnd_from_winid(WIN_STATUS), WM_MSNH_COMMAND, + (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); + strcat(retval, text->buffer); - free(text); + free(text); + } return retval; } @@ -1346,40 +1360,42 @@ nh_compose_unicode_screenshot(void) text = (PMSNHMsgGetText) malloc(sizeof(MSNHMsgGetText) + TEXT_BUFFER_SIZE); - text->max_size = - TEXT_BUFFER_SIZE - - 1; /* make sure we always have 0 at the end of the buffer */ + if (text && retval) { + text->max_size = + TEXT_BUFFER_SIZE + - 1; /* make sure we always have 0 at the end of the buffer */ - wtext = - (PMSNHMsgGetWideText) malloc(sizeof(MSNHMsgGetWideText) - + TEXT_BUFFER_SIZE * sizeof(WCHAR)); - wtext->max_size = - TEXT_BUFFER_SIZE - - 1; /* make sure we always have 0 at the end of the buffer */ + wtext = (PMSNHMsgGetWideText) malloc( + sizeof(MSNHMsgGetWideText) + TEXT_BUFFER_SIZE * sizeof(WCHAR)); + if (wtext) { + wtext->max_size = + TEXT_BUFFER_SIZE + - 1; /* make sure we always have 0 at the end of the buffer */ - ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); - SendMessage(mswin_hwnd_from_winid(WIN_MESSAGE), WM_MSNH_COMMAND, - (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); - retsize += MultiByteToWideChar(CP_ACP, 0, - text->buffer, strlen(text->buffer), - retval + retsize, max_size - retsize); + ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); + SendMessage(mswin_hwnd_from_winid(WIN_MESSAGE), WM_MSNH_COMMAND, + (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); + retsize += MultiByteToWideChar( + CP_ACP, 0, text->buffer, strlen(text->buffer), + retval + retsize, max_size - retsize); - ZeroMemory(wtext->buffer, TEXT_BUFFER_SIZE * sizeof(WCHAR)); - SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_MSNH_COMMAND, - (WPARAM) MSNH_MSG_GETWIDETEXT, (LPARAM) wtext); - wcsncpy(retval + retsize, wtext->buffer, max_size - retsize - 1); - retsize += wcslen(retval + retsize); + ZeroMemory(wtext->buffer, TEXT_BUFFER_SIZE * sizeof(WCHAR)); + SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_MSNH_COMMAND, + (WPARAM) MSNH_MSG_GETWIDETEXT, (LPARAM) wtext); + wcsncpy(retval + retsize, wtext->buffer, max_size - retsize - 1); + retsize += wcslen(retval + retsize); - ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); - SendMessage(mswin_hwnd_from_winid(WIN_STATUS), WM_MSNH_COMMAND, - (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); - retsize += MultiByteToWideChar(CP_ACP, 0, - text->buffer, strlen(text->buffer), - retval + retsize, max_size - retsize); - retval[retsize] = L'\0'; - - free(text); - free(wtext); + ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); + SendMessage(mswin_hwnd_from_winid(WIN_STATUS), WM_MSNH_COMMAND, + (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); + retsize += MultiByteToWideChar( + CP_ACP, 0, text->buffer, strlen(text->buffer), + retval + retsize, max_size - retsize); + retval[retsize] = L'\0'; + free(wtext); + } + free(text); + } return retval; } #endif diff --git a/win/win32/mhmenu.c b/win/win32/mhmenu.c index c66d5d437..defdedbe4 100644 --- a/win/win32/mhmenu.c +++ b/win/win32/mhmenu.c @@ -283,21 +283,22 @@ MenuWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) HDC hdc = GetDC(control); data = (PNHMenuWindow) malloc(sizeof(NHMenuWindow)); - ZeroMemory(data, sizeof(NHMenuWindow)); - data->type = MENU_TYPE_TEXT; - data->how = PICK_NONE; - data->result = 0; - data->done = 0; - data->bmpChecked = - LoadBitmap(GetNHApp()->hApp, MAKEINTRESOURCE(IDB_MENU_SEL)); - data->bmpCheckedCount = - LoadBitmap(GetNHApp()->hApp, MAKEINTRESOURCE(IDB_MENU_SEL_COUNT)); - data->bmpNotChecked = - LoadBitmap(GetNHApp()->hApp, MAKEINTRESOURCE(IDB_MENU_UNSEL)); - data->bmpDC = CreateCompatibleDC(hdc); - data->is_active = FALSE; - SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); - + if (data) { + ZeroMemory(data, sizeof(NHMenuWindow)); + data->type = MENU_TYPE_TEXT; + data->how = PICK_NONE; + data->result = 0; + data->done = 0; + data->bmpChecked = + LoadBitmap(GetNHApp()->hApp, MAKEINTRESOURCE(IDB_MENU_SEL)); + data->bmpCheckedCount = LoadBitmap( + GetNHApp()->hApp, MAKEINTRESOURCE(IDB_MENU_SEL_COUNT)); + data->bmpNotChecked = + LoadBitmap(GetNHApp()->hApp, MAKEINTRESOURCE(IDB_MENU_UNSEL)); + data->bmpDC = CreateCompatibleDC(hdc); + data->is_active = FALSE; + SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); + } /* set font for the text cotrol */ cached_font * font = mswin_get_font(NHW_MENU, ATR_NONE, hdc, FALSE); SendMessage(control, WM_SETFONT, @@ -539,6 +540,7 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) RECT text_rt; HGDIOBJ saveFont; HDC hdc; + TCHAR *was; if (data->type != MENU_TYPE_TEXT) SetMenuType(hWnd, MENU_TYPE_TEXT); @@ -547,12 +549,17 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) text_size = strlen(msg_data->text) + 4; data->menui.text.text = (TCHAR *) malloc(text_size * sizeof(data->menui.text.text[0])); - ZeroMemory(data->menui.text.text, - text_size * sizeof(data->menui.text.text[0])); + if (data->menui.text.text) + ZeroMemory(data->menui.text.text, + text_size * sizeof(data->menui.text.text[0])); } else { + was = data->menui.text.text; + text_size = _tcslen(data->menui.text.text) + strlen(msg_data->text) + 4; data->menui.text.text = (TCHAR *) realloc( data->menui.text.text, text_size * sizeof(data->menui.text.text[0])); + if (!data->menui.text.text) + free(was); /* this is not a good situation, but not a leak either */ } if (!data->menui.text.text) break; @@ -611,75 +618,86 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) break; if (data->menui.menu.size == data->menui.menu.allocated) { + PNHMenuItem was = data->menui.menu.items; + data->menui.menu.allocated += 10; data->menui.menu.items = (PNHMenuItem) realloc( data->menui.menu.items, data->menui.menu.allocated * sizeof(NHMenuItem)); + if (!data->menui.menu.items) + free(was); } - new_item = data->menui.menu.size; - ZeroMemory(&data->menui.menu.items[new_item], - sizeof(data->menui.menu.items[new_item])); - data->menui.menu.items[new_item].glyphinfo = msg_data->glyphinfo; - data->menui.menu.items[new_item].identifier = *msg_data->identifier; - data->menui.menu.items[new_item].accelerator = msg_data->accelerator; - data->menui.menu.items[new_item].group_accel = msg_data->group_accel; - data->menui.menu.items[new_item].attr = msg_data->attr; - data->menui.menu.items[new_item].color = msg_data->color; - strncpy(data->menui.menu.items[new_item].str, msg_data->str, - NHMENU_STR_SIZE); - /* prevent & being interpreted as a mnemonic start */ - strNsubst(data->menui.menu.items[new_item].str, "&", "&&", 0); - data->menui.menu.items[new_item].presel = msg_data->presel; - data->menui.menu.items[new_item].itemflags = msg_data->itemflags; + if (data->menui.menu.items) { + new_item = data->menui.menu.size; + ZeroMemory(&data->menui.menu.items[new_item], + sizeof(data->menui.menu.items[new_item])); + data->menui.menu.items[new_item].glyphinfo = msg_data->glyphinfo; + data->menui.menu.items[new_item].identifier = + *msg_data->identifier; + data->menui.menu.items[new_item].accelerator = + msg_data->accelerator; + data->menui.menu.items[new_item].group_accel = + msg_data->group_accel; + data->menui.menu.items[new_item].attr = msg_data->attr; + data->menui.menu.items[new_item].color = msg_data->color; + strncpy(data->menui.menu.items[new_item].str, msg_data->str, + NHMENU_STR_SIZE); + /* prevent & being interpreted as a mnemonic start */ + strNsubst(data->menui.menu.items[new_item].str, "&", "&&", 0); + data->menui.menu.items[new_item].presel = msg_data->presel; + data->menui.menu.items[new_item].itemflags = msg_data->itemflags; - /* calculate tabstop size */ - hDC = GetDC(hWnd); - cached_font * font = mswin_get_font(NHW_MENU, msg_data->attr, hDC, FALSE); - saveFont = SelectObject(hDC, font->hFont); - GetTextMetrics(hDC, &tm); - p1 = data->menui.menu.items[new_item].str; - p = strchr(data->menui.menu.items[new_item].str, '\t'); - column = 0; - for (;;) { - TCHAR wbuf[BUFSZ]; - RECT drawRect; - SetRect(&drawRect, 0, 0, 1, 1); - if (p != NULL) - *p = '\0'; /* for time being, view tab field as zstring */ - DrawText(hDC, NH_A2W(p1, wbuf, BUFSZ), strlen(p1), &drawRect, - DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_EXPANDTABS - | DT_SINGLELINE); - data->menui.menu.tab_stop_size[column] = - max(data->menui.menu.tab_stop_size[column], - drawRect.right - drawRect.left); + /* calculate tabstop size */ + hDC = GetDC(hWnd); + cached_font *font = + mswin_get_font(NHW_MENU, msg_data->attr, hDC, FALSE); + saveFont = SelectObject(hDC, font->hFont); + GetTextMetrics(hDC, &tm); + p1 = data->menui.menu.items[new_item].str; + p = strchr(data->menui.menu.items[new_item].str, '\t'); + column = 0; + for (;;) { + TCHAR wbuf[BUFSZ]; + RECT drawRect; + SetRect(&drawRect, 0, 0, 1, 1); + if (p != NULL) + *p = '\0'; /* for time being, view tab field as zstring */ + DrawText(hDC, NH_A2W(p1, wbuf, BUFSZ), strlen(p1), &drawRect, + DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_EXPANDTABS + | DT_SINGLELINE); + data->menui.menu.tab_stop_size[column] = + max(data->menui.menu.tab_stop_size[column], + drawRect.right - drawRect.left); - menuitemwidth += data->menui.menu.tab_stop_size[column]; + menuitemwidth += data->menui.menu.tab_stop_size[column]; - if (p != NULL) - *p = '\t'; - else /* last string so, */ - break; + if (p != NULL) + *p = '\t'; + else /* last string so, */ + break; - /* add the separation only when not the last item */ - /* in the last item, we break out of the loop, in the statement - * just above */ - menuitemwidth += TAB_SEPARATION; + /* add the separation only when not the last item */ + /* in the last item, we break out of the loop, in the + * statement just above */ + menuitemwidth += TAB_SEPARATION; - ++column; - p1 = p + 1; - p = strchr(p1, '\t'); + ++column; + p1 = p + 1; + p = strchr(p1, '\t'); + } + + SelectObject(hDC, saveFont); + ReleaseDC(hWnd, hDC); + + /* calculate new menu width */ + data->menui.menu.menu_cx = + max(data->menui.menu.menu_cx, + 2 * TILE_X + menuitemwidth + + (tm.tmAveCharWidth + tm.tmOverhang) * 12); + + /* increment size */ + data->menui.menu.size++; } - SelectObject(hDC, saveFont); - ReleaseDC(hWnd, hDC); - - /* calculate new menu width */ - data->menui.menu.menu_cx = - max(data->menui.menu.menu_cx, - 2 * TILE_X + menuitemwidth - + (tm.tmAveCharWidth + tm.tmOverhang) * 12); - - /* increment size */ - data->menui.menu.size++; } break; case MSNH_MSG_ENDMENU: { diff --git a/win/win32/mhmsgwnd.c b/win/win32/mhmsgwnd.c index d910ed209..7a2529267 100644 --- a/win/win32/mhmsgwnd.c +++ b/win/win32/mhmsgwnd.c @@ -647,6 +647,7 @@ onPaint(HWND hWnd) strcpy(tmptext, data->window_text[i].text); strip_newline(tmptext); NH_A2W(tmptext, wbuf, sizeof(wbuf)); + wbuf[SIZE(wbuf) - 1] = '\0'; wlen = _tcslen(wbuf); setMsgTextColor(hdc, i < (MSG_LINES - data->lines_last_turn)); #ifdef MSG_WRAP_TEXT diff --git a/win/win32/mhrip.c b/win/win32/mhrip.c index fdeefe716..22d22fd6f 100644 --- a/win/win32/mhrip.c +++ b/win/win32/mhrip.c @@ -262,13 +262,20 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) text_size = strlen(msg_data->text) + 4; data->window_text = (TCHAR *) malloc(text_size * sizeof(data->window_text[0])); - ZeroMemory(data->window_text, - text_size * sizeof(data->window_text[0])); + if (data->window_text) { + ZeroMemory(data->window_text, + text_size * sizeof(data->window_text[0])); + } } else { + TCHAR *was = data->window_text; + text_size = _tcslen(data->window_text) + strlen(msg_data->text) + 4; data->window_text = (TCHAR *) realloc( data->window_text, text_size * sizeof(data->window_text[0])); + if (!data->window_text) { + free(was); + } } if (!data->window_text) break; diff --git a/win/win32/mhtext.c b/win/win32/mhtext.c index 524462027..a2e8862ac 100644 --- a/win/win32/mhtext.c +++ b/win/win32/mhtext.c @@ -198,13 +198,20 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) text_size = strlen(msg_data->text) + 4; data->window_text = (TCHAR *) malloc(text_size * sizeof(data->window_text[0])); - ZeroMemory(data->window_text, - text_size * sizeof(data->window_text[0])); + if (data->window_text) { + ZeroMemory(data->window_text, + text_size * sizeof(data->window_text[0])); + } } else { + TCHAR *was = data->window_text; + text_size = _tcslen(data->window_text) + strlen(msg_data->text) + 4; data->window_text = (TCHAR *) realloc( data->window_text, text_size * sizeof(data->window_text[0])); + if (!data->window_text) { + free(was); + } } if (!data->window_text) break; diff --git a/win/win32/mswproc.c b/win/win32/mswproc.c index 1bf470e44..2dd72781d 100644 --- a/win/win32/mswproc.c +++ b/win/win32/mswproc.c @@ -12,7 +12,7 @@ #include "dlb.h" #include "func_tab.h" /* for extended commands */ #include "winMS.h" -#include + #include #include "mhmap.h" #include "mhstatus.h" @@ -1031,11 +1031,17 @@ mswin_putstr_ex(winid wid, int attr, const char *text, int app) /* yield a bit so it gets done immediately */ mswin_get_nh_event(); } else { + char *was = GetNHApp()->saved_text; + // build text to display later in message box GetNHApp()->saved_text = realloc(GetNHApp()->saved_text, strlen(text) + strlen(GetNHApp()->saved_text) + 1); - strcat(GetNHApp()->saved_text, text); + if (!GetNHApp()->saved_text) { + free(was); + } else { + strcat(GetNHApp()->saved_text, text); + } } } @@ -2109,15 +2115,17 @@ mswin_getmsghistory(boolean init) if (init) { text = (PMSNHMsgGetText) malloc(sizeof(MSNHMsgGetText) + TEXT_BUFFER_SIZE); - text->max_size = - TEXT_BUFFER_SIZE - - 1; /* make sure we always have 0 at the end of the buffer */ + if (text) { + text->max_size = + TEXT_BUFFER_SIZE + - 1; /* make sure we always have 0 at the end of the buffer */ - ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); - SendMessage(mswin_hwnd_from_winid(WIN_MESSAGE), WM_MSNH_COMMAND, - (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); + ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); + SendMessage(mswin_hwnd_from_winid(WIN_MESSAGE), WM_MSNH_COMMAND, + (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); - next_message = text->buffer; + next_message = text->buffer; + } } if (!(next_message && next_message[0])) { @@ -2725,27 +2733,29 @@ mswin_color_from_string(char *colorstring, HBRUSH *brushptr, - hexadecimals); ++colorstring; red_value *= 16; - red_value += (int) (strchr(hexadecimals, tolower((uchar) *colorstring)) - - hexadecimals); + red_value += + (int) (strchr(hexadecimals, tolower((uchar) *colorstring)) + - hexadecimals); ++colorstring; - green_value = (int) (strchr(hexadecimals, - tolower((uchar) *colorstring)) - - hexadecimals); + green_value = + (int) (strchr(hexadecimals, tolower((uchar) *colorstring)) + - hexadecimals); ++colorstring; green_value *= 16; - green_value += (int) (strchr(hexadecimals, - tolower((uchar) *colorstring)) - - hexadecimals); + green_value += + (int) (strchr(hexadecimals, tolower((uchar) *colorstring)) + - hexadecimals); ++colorstring; - blue_value = (int) (strchr(hexadecimals, tolower((uchar) *colorstring)) - - hexadecimals); + blue_value = + (int) (strchr(hexadecimals, tolower((uchar) *colorstring)) + - hexadecimals); ++colorstring; blue_value *= 16; - blue_value += (int) (strchr(hexadecimals, - tolower((uchar) *colorstring)) - - hexadecimals); + blue_value += + (int) (strchr(hexadecimals, tolower((uchar) *colorstring)) + - hexadecimals); ++colorstring; *colorptr = RGB(red_value, green_value, blue_value); @@ -2768,7 +2778,10 @@ mswin_color_from_string(char *colorstring, HBRUSH *brushptr, if (max_brush > TOTAL_BRUSHES) panic("Too many colors!"); *brushptr = CreateSolidBrush(*colorptr); - brush_table[max_brush++] = *brushptr; + if (IndexOk(max_brush, brush_table)) { + brush_table[max_brush] = *brushptr; + max_brush++; + } } void