943,945 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3157
  • C RSS
Nov 20th, 2004
0

Weird segfault involving popen

Expand Post »
Hi guys

It has been quite a long time since my last post, but I am having some very weird behaviour on a program I am writing and I was hoping to get a little community feedback

I am writing a class that basically opens a pipe to ksh runs a script gets the results and passes it back to the main program, the weird thing is that as the program terminates it segfaults I know it is related to the class that includes popen but I can't figure out why it is happening:

This is the popen class:
cpp
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <stdio.h>
  4. #include <string>
  5.  
  6. #include "shell_interp.h"
  7.  
  8. using namespace std;
  9.  
  10. Shell_Interp::Shell_Interp()
  11. {
  12. }
  13.  
  14.  
  15. Shell_Interp::~Shell_Interp()
  16. {
  17. }
  18.  
  19. int Shell_Interp::runshellscript(char *filename, std::string& returnme)
  20. {
  21. FILE *kshfd;
  22.  
  23. char line[200]; // Returned values shouldn't be more than char
  24. char *files_to_open;
  25.  
  26. sprintf(files_to_open, "/usr/bin/ksh %s", filename);
  27.  
  28. if( (kshfd = popen(files_to_open, "r")) == NULL )
  29. {
  30. return 1; // Damn file descriptor is invalid
  31. }
  32.  
  33. fgets( line, sizeof(line), kshfd); // Get line not in a loop cause we know the output
  34. std::string return_result(line); // Convert to string to return
  35. returnme = return_result; // Return this
  36.  
  37. pclose(kshfd); // Close file descriptor
  38. return 0;
  39. }

h
  1. #ifndef SHELL_INTERP_H
  2. #define SHELL_INTERP_H
  3.  
  4. #include <string>
  5.  
  6. /**
  7. @author Ben Maynard
  8. */
  9. class Shell_Interp{
  10. public:
  11. Shell_Interp();
  12. ~Shell_Interp();
  13. int runshellscript(char *filename, std::string& returnme);
  14. };
  15.  
  16. #endif

And this is the calling code in the main function (sorry it is very dirty at the moment it is just test code

  1. Shell_Interp my_shell;
  2.  
  3. char *filename;
  4. std::string resulting;
  5. filename = "/home/work/test.sh";
  6.  
  7. std::string getthisdude;
  8.  
  9. int a = my_shell.runshellscript(filename, getthisdude);
  10.  
  11. cout << "Ran shell script to get: " << getthisdude.c_str() << " return was " << a << endl;
  12. my_shell.~ Shell_Interp();
  13.  
  14. return 0;

As it stand the returned string is exactly as expected, it runs everything before this point and everything after this point without a problem, but when it exits I get a segfault, if I remove the popen code no more segfaults so it is within this code here.

Any advice, help, pointers, questions, gratefully recieved.

Thanks

Ben
Similar Threads
Reputation Points: 66
Solved Threads: 3
Junior Poster
liliafan is offline Offline
117 posts
since Apr 2004
Nov 20th, 2004
0

Re: Weird segfault involving popen

files_to_open has no space reserved for it. You declared it as a char* but didn't 'new' it. So do one of these two things:

char* files_to_open = new char[ kMaxFileSize ]; // don't forget to delete it!
-or-
char files_to_open[ kMaxFileSize ];
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Nov 20th, 2004
0

Re: Weird segfault involving popen

Quote originally posted by Chainsaw ...
files_to_open has no space reserved for it. You declared it as a char* but didn't 'new' it. So do one of these two things:

char* files_to_open = new char[ kMaxFileSize ]; // don't forget to delete it!
-or-
char files_to_open[ kMaxFileSize ];
Damn you are good lol:

char files_to_open[ kMaxFileSize ];

Causes failure to compile:
shell_interp.cpp: In member function `int Shell_Interp::runshellscript(char*,
std::string&)':
shell_interp.cpp:31: error: cannot convert `char**' to `char*' for argument `1'
to `int sprintf(char*, const char*, ...)'
shell_interp.cpp:33: error: cannot convert `char**' to `const char*' for
argument `1' to `FILE* popen(const char*, const char*)'

However, char* files_to_open = new char[ kMaxFileSize ]; is perfect gets rid of the segfault completely.

I hate to impose further but any chance you could give me an indication as to why this happened? I would like to learn why this works for future reference instead of just that it does Thanks

Ben
Reputation Points: 66
Solved Threads: 3
Junior Poster
liliafan is offline Offline
117 posts
since Apr 2004
Nov 20th, 2004
0

Re: Weird segfault involving popen

It sounds like you changed

sprintf(files_to_open, "/usr/bin/ksh %s", filename);

to

sprintf(&files_to_open, "/usr/bin/ksh %s", filename);

because that would be a char**, not a char*. Possible?
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Nov 21st, 2004
0

Re: Weird segfault involving popen

Quote originally posted by Chainsaw ...
It sounds like you changed

sprintf(files_to_open, "/usr/bin/ksh %s", filename);

to

sprintf(&files_to_open, "/usr/bin/ksh %s", filename);

because that would be a char**, not a char*. Possible?
Strangely enough no I didn't touch the sprintf() I just changed the declaration of file_to_open.

Ben
Reputation Points: 66
Solved Threads: 3
Junior Poster
liliafan is offline Offline
117 posts
since Apr 2004

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: array declaration explanation needed
Next Thread in C Forum Timeline: Sorry guys. simple Calculator script need





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


Follow us on Twitter


© 2011 DaniWeb® LLC