I agree. This is not comparing pointers; wcsstr compares the strings the
pointers point to, character by character, and returns a pointer to the
character position in the second string where the first string appears.
So, for example, I might want to see if the file has the extension .txt.
So I might use wcsstr(L".txt", filename->Buffer). Now how could this
fail?
***********
Experienced programmers can stop reading here. This is elementary
programming stuff you could do in your sleep.
***********
Well, if I paeed in as the first argument as the Buffer member of a
UNICODE_STRING, it would not be guaranteed to be NUL-terminated, so wcsstr
will happily keep comparing characters. Suppose the UNICODE_STRING.Buffer
has the contents “.txt$” and the filename->Buffer is “myfile.txt%¥?”
Oops.
So, you say, you know that by using the literal, the first argument is
NUL-terminated. So it should work. And indeed it does, if the input is
“myfile.txt”. Even if it is “myfile.txt%¥?”, because it stops when it
finds “.txt” and you don’t overrun the length of the second string
So what happers if the string is “myfile.zip$&:?#%” and the next character
position is the first two bytes of a nonexistent page? Oops. BSOD.
What happens if the garbage I showed is not “$&:?#%” but “$&.txt”?
What if the user passes down “.TXT”?
What if the full path is given, and the string is
"\C\myprivatefiles\my.txtfuiles\oldfiles.zip?
So, you say, I can solve this by putting a NUL at the end of each string.
How are you going to do this? Unless you *know* there is space in the
buffer, you can’t do it. And even if you know there is space in the
buffer by detecting that MaximumLength>Length, you cannot be guaranteed
this will work for every possible input string. Most of the time, you can
expect that Length==MaximumLength (and remember this is a byte count, not
a character count). Bottom line: the CRT string manipulation functions
are, for all practical purposes, mostly useless.
We don’t have a spec on what is going on; we do have a silly assumption
that the input is 8-bit characters that are NUL-terminated, and the buffer
lentgh does not seem to be considered here. I am guessing in the absence
of useful description that this string is sent via DeviceIoControl, which
means the only sane way to do it is to provide both Unicode and 8-bit
versions, e.g.
#define IOCTL_MY_REQUESTA CTL_CODE(…stuff…)
#define IOCTL_MY_REQUESTW CTL_CODE(…slightly different stuff…)
#ifdef UNICODE
#define IOCTL_MY_REQUEST IOCTL_MY_REQUESTW
#else
#define IOCTL_MY_REQUEST IOCTL_MY_REQUESTA
#endif
and that, or some equally functional variant, is really the only
acceptable way to pass a string in. The conversion, 8-bit-to-Unicode, is
only performed for the -A variant. It cannot assume the string is
NUL-terminated. The result, a UNICODE_STRING, is derived from both
requests, and a pointer to that UNICODE_STRING is passed in to the
comparison routine, which must *NOT* be wcsstr. If there is no
equivalent Rtl function that works on UNICODE_STRINGs, then you have to
write one that works correctly.
*sigh*
Us: “You shouldn’t point a gun at your feet!”
You: “I know but it’s ok now, because I’m wearing flip-flops!”
Us: “Uhm… okie dokie.”On Wed, May 29, 2013 at 5:06 AM, wrote:
>> The solution was really easy actually, instead of comparing the Buffer
>> member (which is PWSTR type), i was comparing the pointer directly so
>> that works
>>
>> if(wcsstr(procName->Buffer, proc.Buffer))
>> DbgPrint(“YOUPI !!”);
>>
>> procName is a PUNICODE_STRING which is the current process, and proc is
>> a UNICODE_STRING ( a proc name given from userland)
>>
>> —
>> NTDEV is sponsored by OSR
>>
>> OSR is HIRING!! See http://www.osr.com/careers
>>
>> For our schedule of WDF, WDM, debugging and other seminars visit:
>> http://www.osr.com/seminars
>>
>> To unsubscribe, visit the List Server section of OSR Online at
>> http://www.osronline.com/page.cfm?name=ListServer
>
> —
> NTDEV is sponsored by OSR
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>