Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

segmentation fault with strftime() in client/server program

Posted on 2003-03-18
10
Medium Priority
?
798 Views
Last Modified: 2012-06-21
I'm making a client/server program for my networks class.  In the server.c file, I'm running a loop so that when one client is done and logs out, another can log back in.  Each client asks for the date and time...i'm doing this using the strftime() function.  However, only the first client can get the date and time, then after it logs out if another client calls strftime() I get a segmentation fault.  Is there any way around this? thanks

chad

here's my loop:

       do{
           memset(s2, '\0', sizeof(s2));
           memset(s1, '\0', sizeof(s1));
           read(sd_current, getCom, sizeof(s1));
           time_t t = time(NULL);
           ctime(&t);
           struct tm *tm=localtime(&t);

           if(strcmp(getCom, "date")==0){
    /*fault*/ strftime(sendCom, sizeof(s2), "%a %b %d %Y", tm);
              write(sd_current, sendCom, sizeof(s2));
              memset(s2, '\0', sizeof(s2));
              memset(s1, '\0', sizeof(s1));
           }
           if(strcmp(getCom, "time")==0){
    /*fault*/ strftime(sendCom, sizeof(s2), "%I:%M:%S", tm);
              write(sd_current, sendCom, sizeof(s2));
              memset(s2, '\0', sizeof(s2));
              memset(s1, '\0', sizeof(s1));
           }
        }while(strcmp(getCom, "quit") < 0 || strcmp(getCom, "quit") > 0);

0
Comment
Question by:chadyo
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8162464
you write:

strftime(sendCom, sizeof(s2)...

How big is sendCom?  If it is bigger than s2, that could be where your problem lies...

you should write:

strftime(sendCom, sizeof(sendCom)...

or

strftime(s2, sizeof(s2)...

0
 
LVL 6

Expert Comment

by:gj62
ID: 8162474
by the way, you never store anything in s2, so setting the memory to 0 all the time is a waste of instructions...
0
 

Author Comment

by:chadyo
ID: 8163018
sorry i should have specified this before, sendCom is a pointer to s2, i'm using it as the buffer.

char s2[100];
char *sendCom=s2;

I don't think its anything with the size of the char array.  The date and time aren't that big.  Plus it works the first time through and the messages after that are the same length, just different times.

Thanks for your help though.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:chadyo
ID: 8163152
sorry i should have specified this before, sendCom is a pointer to s2, i'm using it as the buffer.

char s2[100];
char *sendCom=s2;

I don't think its anything with the size of the char array.  The date and time aren't that big.  Plus it works the first time through and the messages after that are the same length, just different times.

Thanks for your help though.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 100 total points
ID: 8164488
Segmentation fault generally occus when you try to reference a memory location which is now free or deallocated. For example, if you are returning a pointer from a function, and let's say that the pointer pointed to a local variable of the function, then after the function execution finishes, the memory allocated for all its local variables is freed and the address returned is now free, not allocated. So effectively, the pointer in the callling function which held the address returned by this called function, is now pointing to garbage.

Since you have not provided your entire code, it is not possible to point out where it is occuring, but can you o through your code and see if the above information helps you to correct your problem.

Mayank.
0
 
LVL 22

Expert Comment

by:grg99
ID: 8166277
What I would do is to write a dummy strftime() that does as little as possible,  like just a strcpy( arg, "foo" );

That will tell us if the problem is truly in styrftime or wether some other pointer is getting messed up.


My guess is: since your pointer "strcom" is declared right after the array it points to, if you somehow overflow your array by even one byte, it's going to zap the pointer, leading to a seg fault the next time you use it.

I would put a "long int MayGetZapped= 0x12345678;"

Between the two variables, then check it with:

#define CheckVars   if( MayGetZapped != 0x12345678 )printf("OOPS!" );

Sprinkle "CheckVars" throughout your code and wait for the OOPS! to appear.

0
 
LVL 2

Assisted Solution

by:honey_hamster
honey_hamster earned 100 total points
ID: 8167733
I changed your source to use the console instead of the read/write socket interface you had, and the code ran fine, so there's nothing inherently wrong in what you're trying to do.  I suggest you ensure that sendCom == s2 before the calls to strftime() to ensure that the sendCom value is valid.

#include <stdio.h>
#include <time.h>

int main( void )
{
  char s1[100];
  char *getCom = s1;
  char s2[100];
  char *sendCom = s2;
 

  do{
          memset(s2, '\0', sizeof(s2));
          memset(s1, '\0', sizeof(s1));
          printf("\n Enter String ");
          gets(getCom);
          time_t t = time(NULL);
          ctime(&t);
          struct tm *tm=localtime(&t);

          if(strcmp(getCom, "date")==0){
   /*fault*/ strftime(sendCom, sizeof(s2), "%a %b %d %Y", tm);
             printf( "\n%s\n", sendCom );
             memset(s2, '\0', sizeof(s2));
             memset(s1, '\0', sizeof(s1));
          }
          if(strcmp(getCom, "time")==0){
   /*fault*/ strftime(sendCom, sizeof(s2), "%I:%M:%S", tm);
             printf( "\n%s\n", sendCom );
             memset(s2, '\0', sizeof(s2));
             memset(s1, '\0', sizeof(s1));
          }
       }while(strcmp(getCom, "quit") < 0 || strcmp(getCom, "quit") > 0);
}

0
 
LVL 20

Expert Comment

by:jmcg
ID: 10144633
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between mayankeagle and honey_hamster.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10144785
Please proceed with that recommendation.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question