Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Lutrosis

Determining location (heap, stack, or string constant) of string at runtime?

This topic is 5245 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''d like to be able to determine whether a char* passed as an argument to a member function points to memory that was allocated at run time, or stores a string constant, from within that member function at run time. In other words, I want to be able to tell the difference between this:
char* str = new char[10];
memset(str, 33, 9); str[9] = ''\0'';
someFunction(str);
 
and this:
someFunction("!!!!!!!!!");
 
at run time. As I understand it, in the first example the memory is on the heap, in the second example its in a ''data segment'' (that''s probably not the correct term, is it?) of the executable, and if you were to do char str[] it would be on the stack. How do I determine which is which? -Lutrosis

Share this post


Link to post
Share on other sites
Advertisement
This is always operating system dependent and sometimes compiler dependent. (And almost always more trouble than it''s worth.)

Share this post


Link to post
Share on other sites
Roger that.

How about this question then:

What I''m looking to do is implement a ''secure'' string class, that removes as much as possible the chance of screwing up string handling and allowing a buffer overrun or some-such. My concern is that even if the string class is implemented just right, if it allows much interaction with c-strings, there''s always potential for, at the least, illegal reads, since you''re depending on the caller to be sure the argument passed in is null-terminated. I figured if there was a way to restrict interaction with c-strings to string constants, this wouldn''t be a problem (the compiler will always null-terminate something in ""''s), and would still give me enough functionality to be useful. Originally I had considered requiring a length parameter for any member function taking a c-string as a param, but on second thought it seems pointless. Either I''d be checking the size of the string in the member function via strlen(), or the caller would probably be doing strClass.someFunction(str, strlen(str)), which would result in the same invalid read for a non-terminated string.

So am I being overly cautious here? Should I just allow reasonable interaction with c-strings?

-Lutrosis

Share this post


Link to post
Share on other sites
No, you''re not really being over cautious. Over writing past the end of a string or forgetting a null terminator are the kind of bugs that will crash your program and leave you debugging fors days because you need one more byte allocated in your char array. However, if you''re using C++, I''d recommend you use the STL(Standard Template Library) string class. It''s very fully featured and safe and it works well with the rest of STL. Plus, it''s worth your time to learn, because it''s quite widely used.

tj963

Share this post


Link to post
Share on other sites
I''m actually already familiar with the STL string class, and many of the other STL classes - I''d like to add some different functionality though, such as (believe it or not) logging, to make those nasty errors you''re talking about easier to find (although if you''re coding for Linux on an x86, a tool called Valgrind makes them pretty darn easy to track down without any extra work).

Plus, the assignment I just finished for my InfoSec class required that we not use any prebuilt string libraries, classes, etc., unless we coded them ourselves

And it''ll be a decent excercise, too.

Thanks for your help,
-Lutrosis

Share this post


Link to post
Share on other sites
In C it''s possible to write code in such a way that a string inside quotation marks will not be null-terminated. So if you intend for your code to interoperate with C code, then even a check to see if the string was statically allocated isn''t good enough. Not only that I could create a char array at the end of the static data area right against a DMZ, initialize it with non null values, and pass it to your function, and boom, read error. Basically with anything as unsafe as a bare pointer, you''re going to have to trust the user to some degree.

The other question would be: how useful would this string class be if you couldn''t intialize it with a C style string that just happened to live in the stack or the heap?

Share this post


Link to post
Share on other sites
quote:
I''d like to be able to determine whether a char* passed as an argument to a member function points to memory that was allocated at run time, or stores a string constant, from within that member function at run time.


There is no way to do so in a platform-independent manner.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!