Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

String handling

Posted on 2005-04-13
7
Medium Priority
?
278 Views
Last Modified: 2010-04-01
Hi,

I'mn debugging an ISAPI wildcard extension used on sharepoint 2001 document libraries.  Basically the code works fine for its main function (creating an audit) but has a conflict of some sort weith the Sharepoint indexing program.  


I found one area where there was a problem and now I have another one!  I have the following code which I put in during debug but want to keep in some form or other -

LPSTR x = (LPSTR) pecb->lpbData;  //pecb->lpbData is of type LPBYTE
string sBrowserCheckoutTest = x;

Basically the problem is that the first line is 'okay'

LPSTR x = (LPSTR) pecb->lpbData;  //pecb->lpbData is of type LPBYTE

but if I add the second line (let alone actually do anything with sBrowserCheckoutTest ) I get an error in the System events log when I run a Sharepoint crawl (index).  I don't know what the error is but it causes a warning to be generated about the application pool.

What I was wondoring is whether it is obvious to anyone whgy this should be ?  I assume this is unlikely so the other question is to ask if someone could

a) confirm that sBrowserCheckoutTest is still in effect a pointer to the original data structure and
b) give me a simple bit of code to copy the byte array (pecb->lpbData ) into a local char array (if possible allowing for the fact that pecb->lpbData can be of varying sizes!!!) so that I am only performing one quick operation on the original data structure.

I would be very grateful if this is possible for anyone to do ... !

Thanks,

Ben.


0
Comment
Question by:gringogordo
7 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13771082
>>>> string sBrowserCheckoutTest = x;

The assignment operator of std::string needs a terminating zero character. If lpbData contains binary data or was read from a stream where no zero character was set, the assignment might read beyond buffer size (what may be is an explanation of the warning).

Regards, Alex
0
 
LVL 3

Accepted Solution

by:
balder earned 1200 total points
ID: 13771137


LPSTR x = (LPSTR) pecb->lpbData;
std::string sBrowserCheckoutTest( x, pecb->lpbDataSize );

guessing you have a pecb->lpbDataSize that tells the size of lpbData.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 800 total points
ID: 13771290
>> give me a simple bit of code to copy the byte array (pecb->lpbData ) into a local char array

   int siz = pecb->lpbDataSize;            // assume you know the size of your data
   LPSTR pstr = new char[siz+1];        // add 1 for terminating zero
   memcpy(pstr, pecb->lpbData, siz);   // copy data
   pstr[siz] = '\0';                               // add the zero character

   .....


   delete [] pstr;        

Note, I would recommend to copy it to a std::string as balder has showed you.

Regards, Alex
                   

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:gringogordo
ID: 13771428
Hi,

Sorry can I just check that I understand what is going on.

1 - std::string sBrowserCheckoutTest( x, pecb->lpbDataSize );  
    ->creates a new char array and copies the exisiting data (x) into it.
2 - string sBrowserCheckoutTest = x;
    -> creates a pointer to the exisiting data

Also

an interesting yet upsetting occurance is that performing the operation in one line stops the warning/error from happening

sDataCheck = (LPSTR) pecb->lpbData;

Is this maybe because (as per itsmeandnobodyelse) there is no null terminator in the byte array and when performing the operation directly like this the string class handles that.  Or is it because I'm allergic to silicon chips ?

Thanks,

Ben.
0
 
LVL 3

Expert Comment

by:balder
ID: 13771501


1 - std::string sBrowserCheckoutTest( x, pecb->lpbDataSize );  
    ->creates a new char array and copies the exisiting data (x) into it.
yepp

2 - string sBrowserCheckoutTest = x;
    -> creates a pointer to the exisiting data
tries to create a new char array, but doesn't know when to stop -> crash

sDataCheck = (LPSTR) pecb->lpbData;

what is sDataCheck?

 LPBYTE sDataCheck;   - then you just get a pointer to the data area
string sDataCheck;  - then you just was lucky, it will no work


0
 
LVL 22

Expert Comment

by:grg99
ID: 13771598
I'd at the very least make sure "x" isnt coming back as NULL.

Then when you're sure it isnt NULL, check to see if its strlen() is reasonable-- maybe somebody forgot to put a terminating '\0' on it.

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13771957
>>>> Or is it because I'm allergic to silicon chips ?

It really seems so as the issue sounds strange  ;-)

There are very few differences between

    LPSTR x = (LPSTR) pecb->lpbData;  //pecb->lpbData is of type LPBYTE
    string sBrowserCheckoutTest = x;

and

    string sDataCheck;
    sDataCheck = (LPSTR) pecb->lpbData;


<<<< LPSTR x = (LPSTR) pecb->lpbData;

Here a debug compilation may find out that lpbData isn't a null-terminated text string within the allocated boundaries of the data buffer.

>>>> sDataCheck = (LPSTR) pecb->lpbData;

That makes a strlen (search for the terminating zero from left to right) on the buffer in lbpData. Cause strlen is a C runtime function it doesn't make a check on buffer boundaries (both Debug and Release). However, if there wasn't a zero character by accident, you would/should see any garbage (unprintable characters) at the end of the string if buffer size was exceeded.

If you couldn't detect such garbage, I would think that the one-liner works by accident, i. e. by changing your code, the data buffer accidantly was initialized by zeroes thus making the text buffer a zero terminated string.

Regards, Alex
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question