He wants *sscanf* not printf. There’s no strsafe equivalent. In fact, as
far as I can tell, there’s no other even vague equivalent available in
kernel mode at all.
Now, I’m not saying that I think string parsing is something that you
should necessarily do in the kernel rather than user mode (though if
you’re doing some kind of network filtering driver it’s unclear that
doing it in user mode is fast enough)…
But surely using a very well understood function like sscanf *correctly*
is better than implementing your *own* buggy string parsing routines.
If you make sure that *every* field is counted and verify (with a
lint-like app) that the parameters are the correct type, I’m not sure
I’d have a better alternative to suggest if you *really* have to do
string parsing in the kernel.
But really, the kernel isn’t designed for string parsing :-)… it’s a
rather inherently dangerous thing to do.
Of course, that won’t stop people, so maybe it’s time for Microsoft to
implement at least a few safe, if perhaps rudimentary, string parsing
routines in the kernel. RtlUnicodeStringToInteger is nice to have, but
it’s a pretty pathetic way to grunge through ascii.
Steve Dispensa wrote:
> We had a driver crash today because of some code that was essentially
> this:
> unsigned char byte;
> sscanf (buffer, “%2x”, &byte);
> So, I fixed that and yelled at the appropriate person.
> But then I wondered why VC++ didn’t error on that.
As everyone else has said, you really shouldn’t be using these
functions. Use the strsafe library - it’s consistent, safe, and
generally much nicer to use than the standard C stuff. If you include
<ntstrsafe.h>, you will get warnings about any use of old C string stuff
> (3790.1830):
>
> ----- Sources:
>
> TARGETNAME=str
> TARGETTYPE=DRIVER
> TARGETPATH=obj
>
> SOURCES=str.c
>
> ------ str.c:
>
> #include <ntddk.h>
> #include <ntstrsafe.h>
>
> NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING
> RegistryPath)
> {
> char buf[1];
> sprintf(buf, “Hello world”);
> //RtlStringCbPrintfA(buf, sizeof(buf), “Hello world”);
> return STATUS_SUCCESS;
> }
>
> -----
>
> C:\dev\str>bcz
> …
> 1>Compiling - str.c for i386
> 1>errors in directory c:\dev\str
> 1>str.c(8) : error C2220: warning treated as error - no object file
> generated
> 1>str.c(8) : error C4995: ‘sprintf’: name was marked as #pragma deprecated
> …
>
> -----
>
> If the safe string version is used (commented out), it will compile
> cleanly and won’t crash. Unfortunately, prefast doesn’t warn about the bug.
>
> More information:
> http://www.microsoft.com/whdc/driver/tips/SafeString.mspx
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/kmarch/hh/kmarch/Other_2fe131c8-834b-4fff-a1c8-3803eeb9324c.xml.asp
>
>
> Note that you have to follow special instructions from one of the URLs
> above if you want to support Win2k with these functions.
>
> -sd
>
>
> ----------------------------------
> Steve Dispensa
> MVP - Windows DDK
> www.kernelmustard.com
>
>
>
–
Ray</ntstrsafe.h></ntddk.h></ntstrsafe.h>