943,578 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 870
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 9th, 2009
0

Visual Studio 2008 Woes

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include "storage_sql.h"
  2.  
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define strcasecmp _stricmp
  7. #include <cstring>
  8. #include <cstdlib>
  9. #include <string>
  10. #include <sstream>
  11. #include <algorithm>
  12. #include "compat/snprintf.h"
  13. #include "common/eventlog.h"
  14. #include "common/util.h"
  15.  
  16. static const char * sql_backslash_to_underscore(const char * key)
  17. {
  18. if (!key) {
  19. ERROR0("got NULL key");
  20. return NULL;
  21. }
  22.  
  23. std::string rep = key;
  24. std::replace(rep.begin(), rep.end(), '\\', '_');
  25. return rep.c_str();
  26. }

The above code returns NULL when I pass it strings like "acct\\username", "acct\\passhash" etc. What gives?
Similar Threads
Reputation Points: 44
Solved Threads: 0
Newbie Poster
stark025 is offline Offline
15 posts
since Jun 2009
Jun 9th, 2009
0

Re: Visual Studio 2008 Woes

Works for me: g++ on Linux
cpp Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<string>
  3.  
  4. static const char * sql_backslash_to_underscore(const char * key)
  5. {
  6. if (!key) {
  7. //ERROR0("got NULL key");
  8. return NULL;
  9. }
  10.  
  11. std::string rep = key;
  12. std::replace(rep.begin(), rep.end(), '\\', '_');
  13. return rep.c_str();
  14. }
  15. int main()
  16. {
  17. std::cout<<sql_backslash_to_underscore("acct\\username");
  18. }

C++ Syntax (Toggle Plain Text)
  1. siddhant3s@Xion:~$ g++ tester.cpp -o tester
  2. siddhant3s@Xion:~$ ./tester
  3. acct_username
Last edited by siddhant3s; Jun 9th, 2009 at 9:50 am.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Jun 9th, 2009
0

Re: Visual Studio 2008 Woes

That's the thing: it works on linux but not on Windows.

Anyway fixed it, code is
C++ Syntax (Toggle Plain Text)
  1. static const char * sql_backslash_to_underscore(const char * key)
  2. {
  3. if (!key) {
  4. ERROR0("got NULL key");
  5. return NULL;
  6. }
  7.  
  8. std::string rep = key;
  9. std::replace(rep.begin(),rep.end(),'\\','_');
  10. return strdup(rep.c_str());
  11. }
Reputation Points: 44
Solved Threads: 0
Newbie Poster
stark025 is offline Offline
15 posts
since Jun 2009
Jun 9th, 2009
0

Re: Visual Studio 2008 Woes

The 1st version was wrong on Windows, Linux or elsewhere. It returns a pointer to deallocated memory of local string data buffer.

The 2nd version works but it's a very dangerous (error-prone) approach to return a pointer to dynamically allocated (by non-standard function strdup) memory. It's so easy to get memory leak and it's too annoying to save returned pointer then free the memory.

Better change this function signature (and code) to
C++ Syntax (Toggle Plain Text)
  1. static std::string sql_backslash_to_underscore(const char * key)
  2. {
  3. std::string rep;
  4. if (key) {
  5. rep = key;
  6. std::replace(rep.begin(),rep.end(),'\\','_');
  7. }
  8. return rep;
  9. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 9th, 2009
0

Re: Visual Studio 2008 Woes

The thing is that the functions calling sql_backslash_to_underscore is expect a const char * instead of a std::string, and I created a copy as a std::string simply so that I can invoke std::replace on it. If there's any function that's identical to std::replace but can work on a const char * and doesn't leak memory, I'd be happy to implement that instead.
Reputation Points: 44
Solved Threads: 0
Newbie Poster
stark025 is offline Offline
15 posts
since Jun 2009
Jun 9th, 2009
0

Re: Visual Studio 2008 Woes

>The thing is that the functions calling sql_backslash_to_underscore is expect a
>const char * instead of a std::string
If you don't have trouble changing the calling code, you can make a function call as : sql_backslash_to_underscore("Hello").c_str();

C-type functions which dealing with Cstrings usually let the caller code determine the output. Most of them has prototype as this:
void f(const char* input, char* output);
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Jun 9th, 2009
0

Re: Visual Studio 2008 Woes

Click to Expand / Collapse  Quote originally posted by siddhant3s ...
>The thing is that the functions calling sql_backslash_to_underscore is expect a
>const char * instead of a std::string
If you don't have trouble changing the calling code, you can make a function call as : sql_backslash_to_underscore("Hello").c_str();

C-type functions which dealing with Cstrings usually let the caller code determine the output. Most of them has prototype as this:
void f(const char* input, char* output);
Wouldn't this replicate the problem earlier? I tried it and it returns blank strings again.
Reputation Points: 44
Solved Threads: 0
Newbie Poster
stark025 is offline Offline
15 posts
since Jun 2009
Jun 9th, 2009
0

Re: Visual Studio 2008 Woes

C++ Syntax (Toggle Plain Text)
  1. static const char * sql_backslash_to_underscore(const char * key)
  2. {
  3. if (!key) {
  4. ERROR0("got NULL key");
  5. return NULL;
  6. }
  7. char * dup = strdup(key);
  8. char* ptr;
  9. while( (ptr = strrchr(dup,'\\')) != NULL)
  10. *ptr = '_';
  11. return dup;
  12. }

One problem with the above code is that the caller cannot call free() on a const char* that is returned by the above function. So unless you typecast to remove the const there will be a memory leak.
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3.  
  4. const char* ptr = sql_backslash_to_underscore("acct\\username\\Program Files\\Debug");
  5. cout << ptr << "\n";
  6. free( (char*)ptr);
  7. }
Last edited by Ancient Dragon; Jun 9th, 2009 at 10:57 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jun 9th, 2009
0

Re: Visual Studio 2008 Woes

>Wouldn't this replicate the problem earlier?
No.
cpp Syntax (Toggle Plain Text)
  1. std::string sql_backslash_to_underscore(const char * key)
  2. {
  3. if (!key) {
  4. //("got NULL key");
  5. return NULL;
  6. }
  7. std::string rep = key;
  8. std::replace(rep.begin(),rep.end(),'\\','_');
  9. std::cout<<(void*)rep.c_str();//lets see what is the location of the cstring of rep
  10. return rep;//note: it is now returning *copy* of rep.
  11. }
  12. int main()
  13. {
  14. const char* a=sql_backslash_to_underscore("acct\\username").c_str();
  15. std::cout<<std::endl<<(void*)a;//check the location of a
  16. std::cout<<std::endl<<a ;
  17. std::cout<<std::endl;
  18. }
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Jun 13th, 2009
0

Re: Visual Studio 2008 Woes

This is the code that calls the function:
C++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3.  
  4. static int _db_get_tab(const char *key, char **ptab, char **pcol)
  5. {
  6. static char tab[DB_MAX_ATTRKEY];
  7. static char col[DB_MAX_ATTRKEY];
  8.  
  9. std::strncpy(tab, key, DB_MAX_TAB - 1);
  10. tab[DB_MAX_TAB - 1] = 0;
  11.  
  12. // no backslash = no table = bad
  13. if (!std::strchr(tab, '\\')) {
  14. return -1;
  15. }
  16.  
  17. // replace the backslash with a terminator
  18. *(std::strchr(tab, '\\')) = 0;
  19.  
  20. // everything after the tab is the column
  21. std::strncpy(col, key + std::strlen(tab) + 1, DB_MAX_TAB - 1);
  22. /* since I edited sql_escape_key to allow backslashes, there now is potential
  23.   * for a backslash to have slipped through, so we will convert it to an
  24.   * underscore, as this was the original behavior
  25.   */
  26. std::strncpy(col, sql_backslash_to_underscore(col), DB_MAX_TAB - 1);
  27. sql_backslash_to_underscore(col), DB_MAX_TAB - 1);] value of col: %s", col);
  28. // terminate the column
  29. col[DB_MAX_TAB - 1] = 0;
  30. *ptab = tab;
  31. *pcol = col;
  32.  
  33. return 0;
  34. }
  35.  

Won't making sql_backslash_to_underscore return strings break this?
Reputation Points: 44
Solved Threads: 0
Newbie Poster
stark025 is offline Offline
15 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Some OOP returning of arrays help.
Next Thread in C++ Forum Timeline: structure of arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC