This C++ code is kind of lame, but I need to maintain it. I cannot seem to figure out a "buffer too small" problem. I am using Visual Studio 2010. I will come up with minimal code required to reproduce based on the values I see in the debugger. Sorry, I will not have tested the actual snippet itself. Also, since my system clipboard is "busy" while I debug, I cannot just copy and paste, so some error might creep in somewhere, but I will double-check stuff. Trust me, you do not want to see the whole function - it is way too long to make any sense :)
From tchar.h
#define _tcsncpy_s wcsncpy_s
From afxstr.h:
typedef ATL::CStringT< TCHAR, StrTraitMFC_DLL< TCHAR > > CString;
From WinNT.h:
typedef WCHAR TCHAR, *PTCHAR;
Oh, man, these macros never seem to end. I will stop here. Finally, from myfile.cpp:
CString str; // Actually a function parameter with value "07/02/2010"
DWORD nIndex = 10;
DWORD nLast = 0;
LPCTSTR psz = (LPCTSTR) str; // Debugger says that it also gets "07/02/2010".
CString s;
_tcsncpy_s(
s.GetBuffer((int) (nIndex - nLast + 1)), // I added the " + 1" part hoping to fix a bug, but that changed nothing
nIndex - nLast,
psz + nLast,
(size_t) (nIndex - nLast)
);
With this I hit an assertion, and the debugger opens tcsncpy_s.inl
with the following code at the end:
53 ...
54 if (available == 0)
55 {
56 if (_COUNT == _TRUNCATE)
57 {
58 _DEST[_SIZE - 1] = 0;
59 _RETURN_TRUNCATE;
60 }
61 RESET_STRING(_DEST, _SIZE);
=>62 _RETURN_BUFFER_TOO_SMALL(_DEST, _SIZE);
63 }
64 _FILL_STRING(_DEST, _SIZE, _SIZE - available + 1);
65 _RETURN_NO_ERROR;
66 }
67
68
The debugger points at line 62: _RETURN_BUFFER_TOO_SMALL
. Unfortunatelly I cannot view the values of things while in the tcsncpy_s.inl
. Perhaps an experienced coder could tell me what is going on here? I believe (perhaps incorrectly) that this code is quite old, and not written with Unicode in mind. What is the best way to fix this sticking to the old N crappy guns (No C++0X tricks or other fancy stuff please) - I just want to put a patch on a bullet wound.