Changeset ee6b30f for message.c


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>
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 */
Note: See TracChangeset for help on using the changeset viewer.