Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6049449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:34:42+00:00 2026-05-23T07:34:42+00:00

I am trying to find an error in a parsing-routine in C. The code

  • 0

I am trying to find an error in a parsing-routine in C. The code is:

  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <time.h>
  #include <sys/time.h>

  static const char * month_abb_names[] =
  {
      "Jan",
      "Feb",
      "Mar",
      "Apr",
      "May",
      "Jun",
      "Jul",
      "Aug",
      "Sep",
      "Oct",
      "Nov",
      "Dec"
  };

  static const char * wday_abb_names[] =
  {
      "Mon",
      "Tue",
      "Wed",
      "Thu",
      "Fri",
      "Sat",
      "Sun",
  };

  time_t mb_mktime(char * time_str)
  {
      struct tm msg_time;
      char * cur, * next, *tmp_cur, *tmp_next, oldval;
      int counter = 0, tmp_counter = 0, i;
      int cur_timezone = 0, sign = 1;
      time_t retval;

      msg_time.tm_isdst = 0;
      cur = time_str;
      next = strchr(cur, ' ');
      while(next)
      {

          oldval = (*next);
          (*next) = '\0';
          switch(counter)
          {
          case 0 :
  // day of week
              for(i = 0; i < 7; i++)
              {
                  if(strncasecmp(cur, wday_abb_names[i], 3) == 0)
                  {
                      msg_time.tm_wday = i +1;
                      break;
                  }
              }
              break;
          case 1 :
  //month name
              for(i = 0; i < 12; i++)
              {
                  if(strncasecmp(cur, month_abb_names[i], 3) == 0)
                  {
                      msg_time.tm_mon = i;
                      break;
                  }
              }
              break;
          case 2 :
  // day of month
              msg_time.tm_mday = strtoul(cur, NULL, 10);
              break;
          case 3 :
  // HH:MM:SS
              tmp_cur = cur;
              tmp_next = strchr(cur, ':');
              tmp_counter = 0;
              while(tmp_next)
              {
                  switch(tmp_counter)
                  {
                  case 0 :
                      msg_time.tm_hour = strtoul(tmp_cur, NULL, 10);
                      break;
                  case 1 :
                      msg_time.tm_min = strtoul(tmp_cur, NULL, 10);
                      break;

                  }
                  tmp_cur = tmp_next + 1;
                  tmp_next =strchr(tmp_cur, ':');
                  tmp_counter++;
              }
              msg_time.tm_sec = strtoul(tmp_cur, NULL, 10);
              break;
          case 4 :
  // timezone
              if( (*cur) == '+')
              {
                  cur++;
              }
              else if ( (*cur) == '-')
              {
                  sign = -1;
                  cur++;
              }
              cur_timezone = (int)strtol(cur, NULL, 10);
              cur_timezone = sign * (cur_timezone / 100) * 60 * 60 + (cur_timezone % 100) * 60;
              break;
          }
          (*next) = oldval;
          cur = next + 1;
          next = strchr(cur, ' ');
          counter++;
      }
  // what's left is year
      msg_time.tm_year = strtoul(cur, NULL, 10) - 1900;

  #ifndef __WIN32
      retval = timegm(&msg_time) - cur_timezone;
  #else
      retval = mktime(&msg_time) - cur_timezone; // + some adjustments....
  #endif
      printf("final msg_time = %ld\n", retval);
      return retval;
  }

  void getTime(char * time_str)
  {
      time_t time = mb_mktime(time_str);
      struct tm  *ts;
      char buf[80];

      /* Format and print the time, "ddd yyyy-mm-dd hh:mm:ss zzz" */
      ts = localtime(&time);
      strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", ts);
      printf("%s --> %s\n", time_str, buf);
  }

  int main()
  {
      getTime("Thu Jun 16 04:53:00 +0000 2011");

      printf("done.");
      return 0;
  }

The main and getTime are new, the mb_mktime is only slightly modified from the original

However, the line ((*next) = '\0';) results in a SIGSEGV. I admit I am fairly uncertain why the code looks like this…

The code however works fine in the normal app.

Can someone explain why this code works in one app and SIGSEGVs in another?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:34:43+00:00Added an answer on May 23, 2026 at 7:34 am

    You should not be modifying a constant string literal, which is "startstring" in this example. I suppose the normal app works because char buffers which are used there are mutable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to find out why this 'json_encode'd string isn't parsing out correctly,
I'm trying to use the str.find() and it keeps raising an error, what am
I'm trying to find the physical pixel size of a string of text. I
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to find the sqlserver adapter for rails on windows. I have tried getting
Trying to find an XML file I can use in lieu of a look-up
Trying to find the best way of create an overlap/overlay layer to fill the
Trying to find a good name for a method swapping each coordinate X and
Trying to find an example that has css rollover using sprites & sliding door
Trying to find a way to send a POST HTTPS request from Python to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.