Visual Studio 2008 Woes

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 5
Reputation: stark025 is an unknown quantity at this point 
Solved Threads: 0
stark025 stark025 is offline Offline
Newbie Poster

Visual Studio 2008 Woes

 
0
  #1
Jun 9th, 2009
  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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Visual Studio 2008 Woes

 
0
  #2
Jun 9th, 2009
Works for me: g++ on Linux
  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. }

  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.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: stark025 is an unknown quantity at this point 
Solved Threads: 0
stark025 stark025 is offline Offline
Newbie Poster

Re: Visual Studio 2008 Woes

 
0
  #3
Jun 9th, 2009
That's the thing: it works on linux but not on Windows.

Anyway fixed it, code is
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Visual Studio 2008 Woes

 
0
  #4
Jun 9th, 2009
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
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: stark025 is an unknown quantity at this point 
Solved Threads: 0
stark025 stark025 is offline Offline
Newbie Poster

Re: Visual Studio 2008 Woes

 
0
  #5
Jun 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Visual Studio 2008 Woes

 
0
  #6
Jun 9th, 2009
>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);
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: stark025 is an unknown quantity at this point 
Solved Threads: 0
stark025 stark025 is offline Offline
Newbie Poster

Re: Visual Studio 2008 Woes

 
0
  #7
Jun 9th, 2009
Originally Posted by siddhant3s View Post
>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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Visual Studio 2008 Woes

 
0
  #8
Jun 9th, 2009
  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Visual Studio 2008 Woes

 
0
  #9
Jun 9th, 2009
>Wouldn't this replicate the problem earlier?
No.
  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. }
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: stark025 is an unknown quantity at this point 
Solved Threads: 0
stark025 stark025 is offline Offline
Newbie Poster

Re: Visual Studio 2008 Woes

 
0
  #10
Jun 13th, 2009
This is the code that calls the function:
  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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 566 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC