From: ajk on
On Fri, 29 Jun 2007 06:08:03 -0700, karim
<karim(a)discussions.microsoft.com> wrote:

>hi,
>
>
>"ajk" wrote:
>
>> On Fri, 29 Jun 2007 03:16:02 -0700, karim
>> <karim(a)discussions.microsoft.com> wrote:
>>
>> >hi ,
>> >the following code has been successfully compiled and executed in vc6.0,and
>> >it has also compiled in vc8.0 but causing problem runtime.
>> >can anybody help me please.........
>> >pNext is of type LPTSTR;
>> >BYTE bVal = 0;
>> > if(_stscanf(pNext,TEXT("%x "), &bVal)==1)
>> >
>> >error: Runtime check Failure #2 stack around the variable 'bVal' was
>> >corrupted.
>> >
>> >-karimulla.
>>
>> try coding that using fgets() instead and in the future avoid any
>> scanf as the plague. scanf is a function that shouldn't be used in
>> modern programs, there are so many other methods to parse a string
>> which are much safer.
>the issuue here is that i am using a BYTE pointer and the method you
>suggested take FILE * and right now i don't have option to change it to File
>* as it was used in many places in the project. can you suggest way to
>correct it using the same method.
>
>-karimulla.
>>
>>


oops it was std sorry, just hadn't used that one.

ok _stscanf()

what I mean was that if you have a buffer already then use string
functions to access the contents instead of any sscanf derivates since
it is so sensitive to the contents of the string. If the string
happens to have other contents than the format specifier indicates you
can get a memory overwrite like you have.

From: Joseph M. Newcomer on
For something like this, I would never consider using _stscanf. It's complete overkill.

bVal = (BYTE)_tcstoul(pNext, NULL, 16);

which is easier to write.

The problem is that you have a BYTE variable but _stscanf is defined as writing an int, so
it writes 32 bits into an 8-bit value, so the code is completely erroneous as written. It
did not work correctly in VS6 either, but VS6 didn't DETECT that the code was completely
bogus (which it is). VS8 detects that this code is incorrect and tells you so. Rewrite
it. If you insist on doing something as complicated as _stscanf for such a trivial
problem, you have to obey its design rules, and these require that the value &bVal be a
32-bit value, which it is not.
joe

On Fri, 29 Jun 2007 03:16:02 -0700, karim <karim(a)discussions.microsoft.com> wrote:

>hi ,
>the following code has been successfully compiled and executed in vc6.0,and
>it has also compiled in vc8.0 but causing problem runtime.
>can anybody help me please.........
>pNext is of type LPTSTR;
>BYTE bVal = 0;
> if(_stscanf(pNext,TEXT("%x "), &bVal)==1)
>
>error: Runtime check Failure #2 stack around the variable 'bVal' was
>corrupted.
>
>-karimulla.
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Joseph M. Newcomer on
See my earlier reply. Your code as written is erroneous, and you never noticed you had
bad code. VS8 won't let you get away with writing bad code. But the real question is why
you are doing something so complicated to solve a simple problem.
joe
On Fri, 29 Jun 2007 06:08:03 -0700, karim <karim(a)discussions.microsoft.com> wrote:

>hi,
>
>
>"ajk" wrote:
>
>> On Fri, 29 Jun 2007 03:16:02 -0700, karim
>> <karim(a)discussions.microsoft.com> wrote:
>>
>> >hi ,
>> >the following code has been successfully compiled and executed in vc6.0,and
>> >it has also compiled in vc8.0 but causing problem runtime.
>> >can anybody help me please.........
>> >pNext is of type LPTSTR;
>> >BYTE bVal = 0;
>> > if(_stscanf(pNext,TEXT("%x "), &bVal)==1)
>> >
>> >error: Runtime check Failure #2 stack around the variable 'bVal' was
>> >corrupted.
>> >
>> >-karimulla.
>>
>> try coding that using fgets() instead and in the future avoid any
>> scanf as the plague. scanf is a function that shouldn't be used in
>> modern programs, there are so many other methods to parse a string
>> which are much safer.
>the issuue here is that i am using a BYTE pointer and the method you
>suggested take FILE * and right now i don't have option to change it to File
>* as it was used in many places in the project. can you suggest way to
>correct it using the same method.
>
>-karimulla.
>>
>>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm