-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PDCurses issue in getch.c function _paste() when PDC_WIDE is defined #295
Comments
… Reported by nhmall. See issue #295.
(Forehead slap) You are correct. Thank you. I verified it with Valgrind just to be completely sure, and it reported the overwriting by four bytes (size of a wide character in Linux) at the end of the array. As expected, using @wmcbrine may be interested, since the same bug affects PDCurses. |
The issue that is still open: those functions are not tested with the existing test programs, otherwise this would have come up before with valgrind. |
ping @Bill-Gray Do you see any option to execute the paste function in a (semi-)automated test to catch possible issues in the future without doing those tests completely manually? |
Um. I could imagine putting text into the clipboard, then doing something along the lines of I suppose we could test for this specific problem, except that we've fixed it, and a test specific to this bug would not necessarily address whatever bugs may remain. |
If nothing else, then it would make "more sure" to not revamp this bug later; but I is fine to say "try to run valgrind through completely testcurs manually" (actually automating this would be quite useful). But I'm fin with whatever you go for :-) |
wincon
PDC_WIDE is defined.
getch.c function _paste() line 276 PDC_getclipboard(&paste, &len) sets the value of len (via PDC_getclipboard()'s call to wcslen). It does not include the terminating null in that value.
The malloc at line 281 does not allocate any space for the trailing null.
getch.c function _paste() line 282 passes the buffer address and that value of len to util.c function PDC_mbstowcs(wpaste, past, len).
In util.c function PDC_mbstowcs, where the value passed from the caller's len is represented by argument n, the following line 455 is encountered.
455: size_t i = mbstowcs(dest, src, n);
The mbstowcs() function returns the number of wide characters
that make up the converted part of the wide-character string, not
including the terminating null wide character.
The value of i and n can match at this point under the circumstances described, and when they do the next line of util.c function PDC_mbstowcs() line 457 writes past the end of the allocated buffer.
457: dest[i] = '\0';
Changing lines 281 and 282 of getch.c function _paste() as follows avoids the write past the end of the buffer.
The situation could be detected in util.c function PDC_mbstowcs() by adding a test for the index i being greater than or equal to the buffer size n above line 457, but it is really too late by then because neither option available for avoiding the write past the end of the buffer would be desirable:
leaving an unterminated string
or silent truncation:
The text was updated successfully, but these errors were encountered: