Terrible, just terrible.
I am assuming here that you defined “data” and used “buffer” but they are
one and the same? So your fragment should read:
WCHAR buffer[10] ;
wchar_t *stopstring ;
double value ;
wcscpy(buffer , L"8.5") ;
value = wcstod(buffer, &stopstring) ;
Code fragments used here many times reflect real code, with “the names
changed to protect your NDA.” Given that, I suspect that the L"8.5" could
very possibly be a dynamic variable input to a function you are using to do
this conversion. You do realize that you have left yourself wide open to a
static
buffer overlfow? Defining buffer as a WCHAR on the stack may or not be bad,
but at no time do you make sure that you are not going to overflow your
buffer.
Let’s say your code really looks like this:
VOID MyFunction(WCHAR inString) {
WCHAR buffer[10] ;
wchar_t *stopstring ;
double value ;
wcscpy(buffer , inString) ;
value = wcstod(buffer, &stopstring) ;
return;
}
You are at the mercy of inString to have a terminator, but if the dork
calling your function didn’t terminate his string, it will appear to be your
function that has failed. If the dork is of the malicious variety he can
easily determine the proper size of inString, overflow buffer and insert his
own return address on the stack. When MyFunction returns, control passes to
the dork’s function and you have been summarily “cracked”.
There are several ways to secure your code from such problems. The way I
suggest is to look for SRTSAFE.H for applications and/or NTSTRSAFE.H for
drivers and use the functions described there. Your code would then appear
like the following fragment written for a kernel driver:
VOID MyFunction(WCHAR inString) {
WCHAR buffer[10] ;
wchar_t *stopstring ;
double value;
NTSTATUS status;
status = RtlStringCbCopyW(buffer , inString) ;
if (NT_SUCCESS(status)) {
value = wcstod(buffer, &stopstring);
return;
}
–
Gary G. Little
Have Computer, Will Travel …
909-698-3191
909-551-2105
http://www.wd-3.com
“unknown name” wrote in message
news:xxxxx@ntdev…
>
> i want to convert a wide character string to double & i am using
> wcstod() function.
> But this function is giving some wague results
> my code snippet
> .
> .
> .
>
> WCHAR buffer[10] ;
> wchar_t *stopstring ;
> double value ;
>
> wcscpy(data , L"8.5") ;
> value = wcstod(buffer, &stopstring) ;
>
> Suppose if i want to convert double precession value to wide
> character string can i use swprint.
> Is this format correct
> swprintf(data , “%2.1lf” , value) ;
> Also this is not converting to string properly.
>
> thanx
>
>
>
>
>
>