I think you probably mean:
const UNICODE_STRING*
and not:
const PUNICODE_STRING
…as the two are not equivalent as to where the const gets applied to (const PUNICODE_STRING == UNICODE_STRING * const, != const UNICODE_STRING *). There is a typedef (PCUNICODE_STRING) that is defined by the headers for that case.
However, note that specifying a UNICODE_STRING as const has limited value, as you can still modify the string through UNICODE_STRING::Buffer.
Most kernel things tend to be annotated nowadays, which provides a much better description of what will happen to static analysis tools than const ever did.
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Joseph M. Newcomer
Sent: Tuesday, November 25, 2008 8:24 PM
To: Windows File Systems Devs Interest List
Subject: RE: [ntfsd] Zero terminated unicode strings?
Yes. Suppose the string was
L"123"
This is stored as the byte sequence (in hex)
31 00 32 00 33 00 00 00
L’1’ L’2’ L’3’ L’\0’
The NUL terminator is added because the definition of the C language
requires it.
If I do
UNICODE_STRING str;
RtlInitUnicodeString(str, L"123");
The UNICODE_STRING structure will reveal that the .Length is 6 bytes, the
.MaximumLength will be 6 bytes, and the .Buffer pointer will point to the
string specified. Note that string literals are normally stored into a
write-protected segment, so an attempt to append to or otherwise modify the
contents of the UNICODE_STRING will, in such a case, cause an access fault.
I do find it odd that most DDI calls do not use the const specification on
UNICODE_STRING values (or pretty much anything other objects) that are not
modified (e.g., IoCreateSymbolicLink specifies two PUNICODE_STRING
arguments, but since it does not modify either, they should be const
PUNICODE_STRING arguments). In general the failure to use const in the
kernel is dismaying.
joe
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Skywing
Sent: Tuesday, November 25, 2008 4:32 PM
To: Windows File Systems Devs Interest List
Subject: RE: [ntfsd] Zero terminated unicode strings?
Just to be extra clear and clarify Joe’s statement here,
RtlInitUnicodeString does NOT include the null terminator in the string that
it has initialized. “The string” is defined as [UnicodeString->Buffer,
(PWCHAR)((PCHAR)UnicodeString->Buffer + UnicodeString->Length)].
The *buffer* may have a null at the end of it, but as far as the
UNICODE_STRING struct is there, it won’t be included in the buffer count.
Joe is referring to the C-style string argument to RtlInitUnicodeString,
which is indeed null terminated. The resultant UNICODE_STRING structure
does not include the terminating null byte in its count, however.
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Joseph M. Newcomer
Sent: Tuesday, November 25, 2008 4:18 PM
To: Windows File Systems Devs Interest List
Subject: RE: [ntfsd] Zero terminated unicode strings?
In general, the only time you ever see a NUL-terminated string in the kernel
is when you use RtlInitUnicodeString to initialize from a string literal.
When returning strings to user space, it is whatever you have told the user
to expect in terms of return value. In general, most I/O operations do not
guarantee NUL-termination, and users are accustomed to writing something
like
BYTE buffer[MAX_SIZE + 1];
if(!ReadFile(h, buffer, MAX_SIZE, &bytesRead, NULL))
… deal with error
else
{
buffer[bytesRead] = 0;
}
so it is rarely an issue. Note also that this works correctly only for ANSI
input (or MAX_SIZE + sizeof(TCHAR) and store two 0 bytes, left as an
Exercise For The Reader, see below)
At least, since I tend to spend most of my life in user space, that’s what I
write, what I see others writing, and what I teach.
Note that the ANSI/Unicode issue does arise, since
ReadFile/WriteFile/DeviceIoControl are byte-oriented and there is no
marshalling based on the type of call (the Registry calls will do
marshalling of Unicode to ANSI on reads and ANSI to Unicode on writes), but
there’s no ReadFileA/ReadFileW etc.
When using the Undocumented Windows API (Nebbett) from user space, there are
UNICODE_STRINGs returned for APIs that return strings, and they are
definitely *not* NUL-terminated. I’ve done this, and examined the return
values with the debugger.
joe
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Lyndon J Clarke
Sent: Tuesday, November 25, 2008 4:01 PM
To: Windows File Systems Devs Interest List
Subject: Re:[ntfsd] Zero terminated unicode strings?
Let me add to the clamour … just forget about zero terminated strings in
kernel mode … and when you got used to that avoid zero terminated strings
in user mode unless these are mandated by the api you are using 
The approach I’ve adopted for passing string data between kernel mode and
user mode is that I have zero terminated unicode string with a character (or
byte) count which includes zero terminator. I dont use the zero terminator
in kernel mode - I use the count. I use the zero terminator in user mode
when I have no choice - if I have no choice, then it’s there. The count is
always the size of the buffer I need to store the thing.
wrote in message news:xxxxx@ntfsd…
> Hi,
>
> I just noticed, through achieving a PAGE_FAULT_IN_NONE_PAGED_AREA BSOD,
> that I’d been slightly foolish.
> I’d assumed in one part of my code that UNICODE_STRINGS were zero
> terminated strings.
> This, certainly isn’t the case for UNICODE_STRINGS returned from a call
> to:
>
> status = FltGetFileNameInformation( pData,
> FLT_FILE_NAME_NORMALIZED |
> FLT_FILE_NAME_QUERY_ALWAYS_ALLOW_CACHE_LOOKUP,
> &pFileNameInformation );
>
> pFileNameInformation->Name is not zero terminated, especially noticable in
> dbg builds (checked) where my memory allocator fills memory with 0xcd on
> allocation.
>
> I noticed this when passing pFileNameInformation->Name to a LPCWCHAR*
> function of my own construction, which assumed a zero terminator - hence
> the 0xffff max size of the buffer was walked passed and a BSOD ensued.
>
> I am now searching for other places where such assumtions about unicode
> strings have been made.
>
> My question is, is this (as has been proved in this instance) the correct
> assumption? Or is there a problem with FltGetFileNameInformation(); //
> highly unlikely.
>
> So, digging Buffer from:
>
> typedef struct _LSA_UNICODE_STRING {
> USHORT Length;
> USHORT MaximumLength;
> PWSTR Buffer;
> } LSA_UNICODE_STRING, *PLSA_UNICODE_STRING, UNICODE_STRING,
> *PUNICODE_STRING;
>
> and passing it to wcslen() for example would { discounting a) being
> completely dumb, as of course thats Length/sizeof(WCHAR) }, also be b) v.
> dangerous.
>
> Just thought I’d post this up for comments.
> I suppose its a case of RTM. However, I’m very surprised I’ve not had this
> type of crash much earlier.
>
> Mike
>
—
NTFSD is sponsored by OSR
For our schedule debugging and file system seminars
(including our new fs mini-filter seminar) visit:
http://www.osr.com/seminars
You are currently subscribed to ntfsd as: xxxxx@flounder.com
To unsubscribe send a blank email to xxxxx@lists.osr.com
–
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
—
NTFSD is sponsored by OSR
For our schedule debugging and file system seminars
(including our new fs mini-filter seminar) visit:
http://www.osr.com/seminars
You are currently subscribed to ntfsd as: xxxxx@valhallalegends.com
To unsubscribe send a blank email to xxxxx@lists.osr.com
—
NTFSD is sponsored by OSR
For our schedule debugging and file system seminars
(including our new fs mini-filter seminar) visit:
http://www.osr.com/seminars
You are currently subscribed to ntfsd as: unknown lmsubst tag argument: ‘’
To unsubscribe send a blank email to xxxxx@lists.osr.com
–
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
—
NTFSD is sponsored by OSR
For our schedule debugging and file system seminars
(including our new fs mini-filter seminar) visit:
http://www.osr.com/seminars
You are currently subscribed to ntfsd as: xxxxx@valhallalegends.com
To unsubscribe send a blank email to xxxxx@lists.osr.com