-
January 30th, 2013, 04:44 AM
#1
[RESOLVED] _stprintf_s - Out of bound access
Code:
WORD iDay;
TCHAR text[3];
_stprintf_s(text, sizeof(text), _T("%d"), iDay);
The above piece of code giving some out of bound access error by some Static code analyser. The iDay is used for Date: ie: 1 - 31.
What should be the proper check to avoid this instead of using a CString in place or TCHAR. ?
◄◄ hypheni ►►
-
January 30th, 2013, 05:28 AM
#2
Re: _stprintf_s - Out of bound access
1. It should be _countof(text) instead of sizeof(text).
From MSDN:
sizeOfBuffer
Maximum number of characters to store.
2. WORD means unsigned short and its max value is 65535 which requires at least 6-character buffer for formatting (5 for digits + 1 for terminating NULL)
Victor Nijegorodov
-
January 30th, 2013, 05:37 AM
#3
Re: _stprintf_s - Out of bound access
Another quick solution would be
Code:
WORD iDay;
TCHAR text[3];
if(iDay <= 31)
_stprintf_s(text, sizeof(text), _T("%d"), iDay);
Correct ?
◄◄ hypheni ►►
-
January 30th, 2013, 05:41 AM
#4
Re: _stprintf_s - Out of bound access
Originally Posted by hypheni
Another quick solution would be
No, that is not a solution. Read up on what a TCHAR is and what sizeof(TCHAR) gives you. That sizeof(text) isn't what you think it is.
Regards,
Paul McKenzie
-
January 30th, 2013, 05:43 AM
#5
-
January 30th, 2013, 05:43 AM
#6
Re: _stprintf_s - Out of bound access
Okay, Correct I missed it.
◄◄ hypheni ►►
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|