Segmentation Fault - Core Dumped.

Hi

i am running gcc under unix, when i compile the below source code and execute it under command mode, i get the required output. If the same executable is executed under cron, i am getting an error as "Segmentation Fault - Core Dumped". Could any one help me why i am getting this.

---source start---

#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;

int main(int argc, char* argv[]) {
  if ( argc != 2 ) {
   cout << endl << " Usage : " << argv[0] << " <userid>" << endl ;
   cout << " This will delete userid mails from your inbox" << endl ;
   exit(1) ;
  }
  string line ;
  char* usr = getlogin();
  char* inpath="/var/mail/";
  char* tmppath="/tmp/";
  char* inpfl = new char[strlen(usr)+strlen(inpath)+1];
  char* outfl = new char[strlen(usr)+strlen(tmppath)+1];
  strcpy(inpfl, inpath);
  strcat(inpfl, usr);
  strcpy(outfl, tmppath);
  strcat(outfl, usr);
  strcat(outfl, ".tmp");

  ifstream in(inpfl) ;
  ofstream out(outfl) ;
  int found = 0 ;

  while (getline(in,line)) {
   string findW(line) ;
   int strfound = findW.find("From ") ;
   if ( strfound == 0 ) {
      int tstfound = findW.find(string("From ") + argv[1]) ;
     if ( tstfound == 0 )
      found = 1;
     else
     {
      out << line << endl ;
      found = 0 ;
      }
    }
    else
    if ( found == 0 )
      out << line << endl  ;
  }

  char* mvstring = new char[strlen(outfl)+strlen(inpfl)+1];
  strcpy(mvstring,"mv ") ;
  strcat(mvstring,outfl) ;
  strcat(mvstring," ") ;
  strcat(mvstring,inpfl) ;
  system(mvstring) ;
}                                                                                                                                    

---source end---

Note : I doubt the reason to core error is that the system could not recognise the loginname when run under cron, if this is so how to overcome this, is there any way to get the loginname.

Sorry for long summary. helps appreciated.

--gsbabu
gsbabuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
Can't you debug it to determine if that is the problem?  and if not to determine where in the program it is crashing.
0
seredaCommented:
You have an error when allocating memory to char strings:

char* outfl = new char[strlen(usr)+strlen(tmppath)+1];
strcpy(outfl, tmppath);
strcat(outfl, usr);
strcat(outfl, ".tmp");

You forgot to count ".tmp" string - it takes bytes also.

The same error with mvstring.

I suggest that you don't use char*, use class string instead.


 
0
_lychee_Commented:
dunno if this will help, but

char* outfl = new char[strlen(usr)+strlen(tmppath)+1];
 
is too short... cos u add ".tmp" so it should be +5 instead of +1...

similar prob with
char* mvstring = new char[strlen(outfl)+strlen(inpfl)+1];
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

seredaCommented:
To be more concrete:
change respective strings to:
char* outfl = new char[strlen(usr)+strlen(tmppath)+5];

char* mvstring = new char[strlen(outfl)+strlen(inpfl)+5];
 

0
gsbabuAuthor Commented:
Hi

Even after increasing the size, i do get the core error. is there any other way do i get the loginname.

-gsbabu
0
seredaCommented:
Then you should compile the program with debugging (-g for gcc), then run it, get core file, run gdb (gnu debugger) with core file and your binary, then print out stack trace and find out where does your code goes off.

The other way:
add
   printf ("main() line 1");
at line 1, line 2 at line 2, etc...
then run the program and see where do you get until crash. Then you'll get the line with error.

After that, post that line here and i will help you find out the bug.
0
nietodCommented:
We need to know where it is crashing.
0
jkrCommented:
From the 'getlogin()' manpage:


       These  functions  let your program identify positively the
       user who is running (cuserid) or the user  who  logged  in
       this  session  (getlogin).   (These can differ when setuid
       programs are involved.)

       For most purposes, it is more useful to use  the  environ­
       ment  variable  LOGNAME to find out who the user is.  This
       is more flexible precisely because the user can  set  LOG­NAME arbitrarily.


It's most likely that you're experiencing this problem...

change

  char* usr = getlogin();


to read

  char* usr = getenv("LOGNAME");

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gsbabuAuthor Commented:
Hi sereda,

Even after adding the print lines, it goes on print till the end. but when i do "strings core" i see the following(will it be helpful).

--
main() line 18
main() line 19
main() line 20
main() line 21
main() line 22
main() line 23
out of memory
n > max_size ()
pos > len
len - n1 > max_size () - n2                                                    
....
B7Hv
Cv4W
Infinity
coCa?
8stdiobuf
12istdiostream
12ostdiostream
Internal Compiler Bug: No runtime type matcher.
9exception
13bad_exception
bad_alloc
bad_alloc
bad_alloc
....

----
0
seredaCommented:
No, strings won't be much help.
As i got it, your program prints all debug strings till the end of main() and then dumps core?
Then this happens in local or global object destructor, presumably in string.
0
gsbabuAuthor Commented:
Hi jkr,

Initially it worked perfectly, but i will make one more count for the cron to execute. anyhow the points are for you. thanks for your comment. if you don't mind can i know your e-mail id. thanks once again.

--gsbabu
ugnasu01@thiru.vetri.com
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.