Proper Allocation and Deallocating of Unicode String

Dear All,
Just a suggestion will be of great help! Is it fine to check memory while
allocation and deallocating like:::::::

UNICODE_STRING tempA;
irpStack = IoGetCurrentIrpStackLocation( Irp );
fileObject = irpStack->FileObject;
fileName = &fileObject->FileName;
devExt = DeviceObject->DeviceExtension;

//////////////While allocating memory to a string////////////////////

tempA.MaximumLength = fileName->MaximumLength +
devExt->DeviceName.MaximumLength + 2;
tempA.Buffer = ExAllocatePoolWithTag(NonPagedPool,
tempA.MaximumLength,‘2leD’);

if(NULL == tempA.Buffer) {
DbgPrint(“\xxxxx@LACK OF RESOURCES…”);
return STATUS_INSUFFICIENT_RESOURCES;
}

RtlCopyUnicodeString(&tempA,&devExt->DeviceName);
RtlAppendUnicodeStringToString(&tempA, fileName);
////////////////////////////

////////While deallocating ///////
if(tempA.Buffer){
ExFreePoolWithTag(tempA.Buffer,‘pmoC’);
}

/////////////////////////////////////////

I look forward to your valuable comments.

Regards,
Rohit

Perfect.

Regards,
Satish K.S

UNICODE_STRING tempA;
irpStack = IoGetCurrentIrpStackLocation( Irp );
fileObject = irpStack->FileObject;
fileName = &fileObject->FileName;
devExt = DeviceObject->DeviceExtension;

//////////////While allocating memory to a string////////////////////

tempA.MaximumLength = fileName->MaximumLength +
devExt->DeviceName.MaximumLength + 2;
tempA.Buffer = ExAllocatePoolWithTag(NonPagedPool,
tempA.MaximumLength,‘2leD’);

if(NULL == tempA.Buffer) {
DbgPrint(“\xxxxx@LACK OF RESOURCES…”);
return STATUS_INSUFFICIENT_RESOURCES;
}

RtlCopyUnicodeString(&tempA,&devExt->DeviceName);
RtlAppendUnicodeStringToString(&tempA, fileName);
////////////////////////////

////////While deallocating ///////
if(tempA.Buffer){
ExFreePoolWithTag(tempA.Buffer,‘pmoC’);
}

/////////////////////////////////////////

I look forward to your valuable comments.

Regards,
Rohit


Questions? First check the IFS FAQ at
https://www.osronline.com/article.cfm?id=17

You are currently subscribed to ntfsd as: xxxxx@liqwidkrystal.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

At the first look, the code seems fine. You only have to
be careful that the file object must not have a name
(e.g. when a volume is open).
Also if the code is supposed to run as a dispatch
handler of some IRP request,
remember that you have not only to return
an error code, but you have also to complete
the request using IoCompleteRequest (and giving
the same error code).

L.

Dear Mr Zezula,
What do you mean by

Also if the code is supposed to run as a dispatch handler of some IRP
request,???

Regards,
Rohit

“Ladislav Zezula” wrote in message news:xxxxx@ntfsd…
> At the first look, the code seems fine. You only have to
> be careful that the file object must not have a name
> (e.g. when a volume is open).
> Also if the code is supposed to run as a dispatch
> handler of some IRP request,
> remember that you have not only to return
> an error code, but you have also to complete
> the request using IoCompleteRequest (and giving
> the same error code).
>
> L.
>

Dear All,

Do you mean to say that the check should be (since i am using them in my irp
dispatch routines) :::
////////
if(NULL == tempA.Buffer) {

DbgPrint(“\xxxxx@LACK OF RESOURCES…”);
status = STATUS_INSUFFICIENT_RESOURCES;
Irp->IoStatus.Status = status;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}
////////////

If you leave routine when you failed to allocate memory then you don’t have
to check if your buffer pointer is NULL before deallocating it. I.e., if you
have these lines:

if(NULL == tempA.Buffer) {
DbgPrint(“\xxxxx@LACK OF RESOURCES…”);
return STATUS_INSUFFICIENT_RESOURCES;
}

…then you do not need these ones:

////////While deallocating ///////
if(tempA.Buffer){

Also, do not forget to complete request if this code is in dispatch routine,
like this:

if (NULL == tempA.Buffer)
{
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
return STATUS_INSUFFICIENT_RESOURCES;
}

–htfv

“Rohit Dhamija” wrote in message
news:xxxxx@ntfsd…
>
> Dear All,
> Just a suggestion will be of great help! Is it fine to check memory while
> allocation and deallocating like:::::::
>
> UNICODE_STRING tempA;
> irpStack = IoGetCurrentIrpStackLocation( Irp );
> fileObject = irpStack->FileObject;
> fileName = &fileObject->FileName;
> devExt = DeviceObject->DeviceExtension;
>
>
> //////////////While allocating memory to a string////////////////////
>
> tempA.MaximumLength = fileName->MaximumLength +
> devExt->DeviceName.MaximumLength + 2;
> tempA.Buffer = ExAllocatePoolWithTag(NonPagedPool,
> tempA.MaximumLength,‘2leD’);
>
> if(NULL == tempA.Buffer) {
> DbgPrint(“\xxxxx@LACK OF RESOURCES…”);
> return STATUS_INSUFFICIENT_RESOURCES;
> }
>
> RtlCopyUnicodeString(&tempA,&devExt->DeviceName);
> RtlAppendUnicodeStringToString(&tempA, fileName);
> ////////////////////////////
>
> ////////While deallocating ///////
> if(tempA.Buffer){
> ExFreePoolWithTag(tempA.Buffer,‘pmoC’);
> }
>
>
> /////////////////////////////////////////
>
> I look forward to your valuable comments.
>
> Regards,
> Rohit
>
>
>

Dear Mr Logachyov,

Thanks! That was very helpful!

Regards,
Rohit
“Alexey Logachyov” wrote in message news:xxxxx@ntfsd…
> If you leave routine when you failed to allocate memory then you don’t
have
> to check if your buffer pointer is NULL before deallocating it. I.e., if
you
> have these lines:
>
> > if(NULL == tempA.Buffer) {
> > DbgPrint(“\xxxxx@LACK OF RESOURCES…”);
> > return STATUS_INSUFFICIENT_RESOURCES;
> > }
>
> …then you do not need these ones:
>
> > ////////While deallocating ///////
> > if(tempA.Buffer){
>
> Also, do not forget to complete request if this code is in dispatch
routine,
> like this:
>
> if (NULL == tempA.Buffer)
> {
> Irp->IoStatus.Information = 0;
> Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
> return STATUS_INSUFFICIENT_RESOURCES;
> }
>
> --htfv
>
>
>
> “Rohit Dhamija” wrote in message
> news:xxxxx@ntfsd…
> >
> > Dear All,
> > Just a suggestion will be of great help! Is it fine to check memory
while
> > allocation and deallocating like:::::::
> >
> > UNICODE_STRING tempA;
> > irpStack = IoGetCurrentIrpStackLocation( Irp );
> > fileObject = irpStack->FileObject;
> > fileName = &fileObject->FileName;
> > devExt = DeviceObject->DeviceExtension;
> >
> >
> > //////////////While allocating memory to a string////////////////////
> >
> > tempA.MaximumLength = fileName->MaximumLength +
> > devExt->DeviceName.MaximumLength + 2;
> > tempA.Buffer = ExAllocatePoolWithTag(NonPagedPool,
> > tempA.MaximumLength,‘2leD’);
> >
> > if(NULL == tempA.Buffer) {
> > DbgPrint(“\xxxxx@LACK OF RESOURCES…”);
> > return STATUS_INSUFFICIENT_RESOURCES;
> > }
> >
> > RtlCopyUnicodeString(&tempA,&devExt->DeviceName);
> > RtlAppendUnicodeStringToString(&tempA, fileName);
> > ////////////////////////////
> >
> > ////////While deallocating ///////
> > if(tempA.Buffer){
> > ExFreePoolWithTag(tempA.Buffer,‘pmoC’);
> > }
> >
> >
> > /////////////////////////////////////////
> >
> > I look forward to your valuable comments.
> >
> > Regards,
> > Rohit
> >
> >
> >
>
>
>