Changeset ee6b30f


Ignore:
Timestamp:
Oct 8, 2017, 6:02:19 PM (7 years ago)
Author:
Anders Kaseorg <andersk@mit.edu>
Branches:
master
Children:
4fd3c04
Parents:
09530e6
git-author:
Anders Kaseorg <andersk@mit.edu> (10/07/17 03:25:44)
git-committer:
Anders Kaseorg <andersk@mit.edu> (10/08/17 18:02:19)
Message:
Avoid thread-unsafe functions ctime, localtime, strtok

BarnOwl seems to be growing threads at an alarming rate, so let’s be
sure these don’t turn into race conditions.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • functions.c

    rb61ad80 ree6b30f  
    12261226  FILE *file;
    12271227  time_t now;
     1228  struct tm tm;
    12281229  va_list ap;
    12291230
     
    12371238  now = time(NULL);
    12381239
    1239   tmpbuff = owl_util_format_time(localtime(&now));
     1240  tmpbuff = owl_util_format_time(localtime_r(&now, &tm));
    12401241  fprintf(file, "[%d -  %s - %lds]: ",
    12411242          (int) getpid(), tmpbuff, now - owl_global_get_starttime(&g));
     
    17751776  char buff[MAXPATHLEN+1];
    17761777  time_t start;
     1778  struct tm tm;
    17771779  int up, days, hours, minutes;
    17781780  owl_fmtext fm;
     
    18001802  owl_fmtext_append_normal(&fm, "\n");
    18011803
    1802   tmpbuff = owl_util_format_time(localtime(&start));
     1804  tmpbuff = owl_util_format_time(localtime_r(&start, &tm));
    18031805  owl_fmtext_appendf_normal(&fm, "  Startup Time: %s\n", tmpbuff);
    18041806  g_free(tmpbuff);
     
    34243426  char *date;
    34253427  time_t now;
     3428  struct tm tm;
    34263429  char *buff;
    34273430
    34283431  now = time(NULL);
    3429   date = owl_util_format_time(localtime(&now));
     3432  date = owl_util_format_time(localtime_r(&now, &tm));
    34303433
    34313434  buff = g_strdup_printf("%s %s", date, string);
  • message.c

    rff528e6 ree6b30f  
    3030void owl_message_init(owl_message *m)
    3131{
     32  /* ctime_r requires a 26-byte buffer */
     33  char timestr[26];
     34
    3235  m->id=owl_global_get_nextmsgid(&g);
    3336  owl_message_set_direction_none(m);
     
    4346  /* save the time */
    4447  m->time = time(NULL);
    45   m->timestr = g_strdup(ctime(&m->time));
    46   m->timestr[strlen(m->timestr)-1] = '\0';
     48  ctime_r(&m->time, timestr);
     49  m->timestr = g_strndup(timestr, strlen(timestr) - 1);
    4750
    4851  m->fmtext = NULL;
     
    349352CALLER_OWN char *owl_message_format_time(const owl_message *m)
    350353{
    351   return owl_util_format_time(localtime(&m->time));
     354  struct tm tm;
     355  return owl_util_format_time(localtime_r(&m->time, &tm));
    352356}
    353357
     
    593597CALLER_OWN GList *owl_message_get_cc_without_recipient(const owl_message *m)
    594598{
    595   char *cc, *shortuser, *recip;
     599  char *cc, *shortuser, *recip, *saveptr;
    596600  const char *user;
    597601  GList *out = NULL;
     
    603607  recip = short_zuser(owl_message_get_recipient(m));
    604608
    605   user = strtok(cc, " ");
     609  user = strtok_r(cc, " ", &saveptr);
    606610  while (user != NULL) {
    607611    shortuser = short_zuser(user);
     
    610614    }
    611615    g_free(shortuser);
    612     user = strtok(NULL, " ");
     616    user = strtok_r(NULL, " ", &saveptr);
    613617  }
    614618
     
    775779  struct hostent *hent;
    776780#endif /* ZNOTICE_SOCKADDR */
    777   char *tmp, *tmp2;
     781  /* ctime_r requires a 26-byte buffer */
     782  char timestr[26], *tmp, *tmp2;
    778783  int len;
    779784
     
    793798  if (m->timestr) g_free(m->timestr);
    794799  m->time = n->z_time.tv_sec;
    795   m->timestr = g_strdup(ctime(&m->time));
    796   m->timestr[strlen(m->timestr)-1] = '\0';
     800  ctime_r(&m->time, timestr);
     801  m->timestr = g_strndup(timestr, strlen(timestr) - 1);
    797802
    798803  /* set other info */
  • zephyr.c

    rff58239 ree6b30f  
    284284  FILE *file;
    285285  int fopen_errno;
    286   char *tmp, *start;
     286  char *tmp, *start, *saveptr;
    287287  char *buffer = NULL;
    288288  char *subsfile;
     
    319319   
    320320    /* add it to the list of subs */
    321     if ((tmp = strtok(start, ",\n\r")) == NULL)
     321    if ((tmp = strtok_r(start, ",\n\r", &saveptr)) == NULL)
    322322      continue;
    323323    subs[count].zsub_class = g_strdup(tmp);
    324     if ((tmp=strtok(NULL, ",\n\r")) == NULL)
     324    if ((tmp = strtok_r(NULL, ",\n\r", &saveptr)) == NULL)
    325325      continue;
    326326    subs[count].zsub_classinst = g_strdup(tmp);
    327     if ((tmp = strtok(NULL, " \t\n\r")) == NULL)
     327    if ((tmp = strtok_r(NULL, " \t\n\r", &saveptr)) == NULL)
    328328      continue;
    329329    subs[count].zsub_recipient = g_strdup(tmp);
Note: See TracChangeset for help on using the changeset viewer.