]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
introduce strlcpy() and strlcat() functions and use them
authorJaroslav Kysela <perex@perex.cz>
Wed, 20 Jun 2018 12:23:43 +0000 (14:23 +0200)
committerJaroslav Kysela <perex@perex.cz>
Wed, 20 Jun 2018 12:50:54 +0000 (14:50 +0200)
43 files changed:
src/access.c
src/channels.c
src/descrambler/descrambler.c
src/dvr/dvr_autorec.c
src/dvr/dvr_db.c
src/dvr/dvr_rec.c
src/dvr/dvr_timerec.c
src/dvr/dvr_vfsmgr.c
src/epggrab/channel.c
src/epggrab/module.c
src/epggrab/module/eit.c
src/epggrab/module/eitpatternlist.c
src/epggrab/module/xmltv.c
src/esfilter.c
src/filebundle.c
src/http.c
src/idnode.c
src/imagecache.c
src/input/mpegts/dvb_psi.c
src/input/mpegts/en50221/en50221_apps.c
src/input/mpegts/iptv/iptv.c
src/input/mpegts/iptv/iptv_mux.c
src/input/mpegts/mpegts_input.c
src/input/mpegts/mpegts_network.c
src/input/mpegts/satip/satip_frontend.c
src/input/mpegts/scanfile.c
src/intlconv.c
src/lang_str.c
src/rtsp.c
src/satip/rtp.c
src/satip/rtsp.c
src/service.c
src/settings.c
src/spawn.c
src/tcp.c
src/tvh_locale.c
src/tvheadend.h
src/tvhlog.c
src/tvhlog.h
src/udp.c
src/url.c
src/wizard.c
src/wrappers.c

index f652752afb5c89e7a18729ae9b4296c0e24449a3..bf93919c53df43c85adf2ab9e18098f91bc7ef7f 100644 (file)
@@ -885,8 +885,7 @@ access_set_prefix(struct access_ipmask_queue *ais, const char *prefix, int dflt)
     free(ai);
   }
 
-  strncpy(tokbuf, prefix, sizeof(tokbuf)-1);
-  tokbuf[sizeof(tokbuf) - 1] = 0;
+  strlcpy(tokbuf, prefix, sizeof(tokbuf));
   tok = strtok_r(tokbuf, delim, &saveptr);
 
   while (tok != NULL) {
index 9cf5ad43c5cee5852f8c298b3568512717296d34..7e0c24bcc3c9b89b729e62fdb0364c1391c4dc68 100644 (file)
@@ -1166,8 +1166,7 @@ found:
   if ((id = imagecache_get_id(icon))) {
     snprintf(buf, sizeof(buf), "imagecache/%d", id);
   } else {
-    strncpy(buf, icon, sizeof(buf));
-    buf[sizeof(buf)-1] = '\0';
+    strlcpy(buf, icon, sizeof(buf));
   }
 
   return buf;
index 15b54a05ef2cd4464708c0beffdbdd1f591d678e..f6ea0fe6a78dd58501215fde6c1cf266fb1a56da 100644 (file)
@@ -527,10 +527,10 @@ descrambler_notify( th_descrambler_t *td,
   di->provid  = provid;
   di->ecmtime = ecmtime;
   di->hops    = hops;
-  strncpy(di->cardsystem, cardsystem, sizeof(di->cardsystem)-1);
-  strncpy(di->reader, reader, sizeof(di->reader)-1);
-  strncpy(di->from, from, sizeof(di->protocol)-1);
-  strncpy(di->protocol, protocol, sizeof(di->protocol)-1);
+  strlcpy(di->cardsystem, cardsystem, sizeof(di->cardsystem));
+  strlcpy(di->reader, reader, sizeof(di->reader));
+  strlcpy(di->from, from, sizeof(di->protocol));
+  strlcpy(di->protocol, protocol, sizeof(di->protocol));
 
   pthread_mutex_lock(&t->s_stream_mutex);
   descrambler_notify_deliver(t, di);
index 1e1f4c9153975c5afb3bfebf374bd2e400e8e5d5..17413b422737bd8a2f957eca9435a7b88a1ccc9d 100644 (file)
@@ -665,7 +665,7 @@ dvr_autorec_entry_class_time_get(void *o, int tm)
   if (tm >= 0)
     snprintf(prop_sbuf, PROP_SBUF_LEN, "%02d:%02d", tm / 60, tm % 60);
   else
-    strncpy(prop_sbuf, N_("Any"), 16);
+    strlcpy(prop_sbuf, N_("Any"), PROP_SBUF_LEN);
   return &prop_sbuf_ptr;
 }
 
index a342b0cd54581c442cdb4f0a14af6315180e9876..e1a087128a9f780ad1b6f39a56401e327ad72744 100644 (file)
@@ -3392,8 +3392,7 @@ static const void *
 dvr_entry_class_status_get(void *o)
 {
   dvr_entry_t *de = (dvr_entry_t *)o;
-  strncpy(prop_sbuf, dvr_entry_status(de), PROP_SBUF_LEN);
-  prop_sbuf[PROP_SBUF_LEN-1] = '\0';
+  strlcpy(prop_sbuf, dvr_entry_status(de), PROP_SBUF_LEN);
   return &prop_sbuf_ptr;
 }
 
@@ -3401,8 +3400,7 @@ static const void *
 dvr_entry_class_sched_status_get(void *o)
 {
   dvr_entry_t *de = (dvr_entry_t *)o;
-  strncpy(prop_sbuf, dvr_entry_schedstatus(de), PROP_SBUF_LEN);
-  prop_sbuf[PROP_SBUF_LEN-1] = '\0';
+  strlcpy(prop_sbuf, dvr_entry_schedstatus(de), PROP_SBUF_LEN);
   return &prop_sbuf_ptr;
 }
 
index 677c52124931a5af3d46594acadc21305977c953..3a539faa7bdceb1b1f4927a3df753726ef3699c9 100644 (file)
@@ -323,8 +323,7 @@ dvr_do_prefix(const char *id, const char *fmt, const char *s, char *tmp, size_t
   } else if (s[0] && !isalpha(id[0])) {
     snprintf(tmp, tmplen, "%c%s", id[0], s);
   } else {
-    strncpy(tmp, s, tmplen-1);
-    tmp[tmplen-1] = '\0';
+    strlcpy(tmp, s, tmplen);
   }
   return dvr_clean_directory_separator(tmp, tmp, tmplen);
 }
@@ -805,8 +804,7 @@ dvr_sub_str(const char *id, const char *fmt, const void *aux, char *tmp, size_t
 static const char *
 dvr_sub_str_separator(const char *id, const char *fmt, const void *aux, char *tmp, size_t tmplen)
 {
-  strncpy(tmp, (const char *)aux, tmplen-1);
-  tmp[tmplen-1] = '\0';
+  strlcpy(tmp, (const char *)aux, tmplen);
   return dvr_clean_directory_separator(tmp, tmp, tmplen);
 }
 
@@ -843,8 +841,7 @@ static htsstr_substitute_t dvr_subs_postproc_entry[] = {
 static const char *
 dvr_sub_basename(const char *id, const char *fmt, const void *aux, char *tmp, size_t tmplen)
 {
-  strncpy(tmp, (const char *)aux, tmplen);
-  tmp[tmplen-1] = '\0';
+  strlcpy(tmp, (const char *)aux, tmplen);
   return basename(tmp);
 }
 
@@ -948,8 +945,7 @@ pvr_generate_filename(dvr_entry_t *de, const streaming_start_t *ss)
 
   localtime_r(&de->de_start, &tm);
 
-  strncpy(path, cfg->dvr_storage, sizeof(path));
-  path[sizeof(path)-1] = '\0';
+  strlcpy(path, cfg->dvr_storage, sizeof(path));
   l = strlen(path);
   if (l + 1 >= sizeof(path)) {
     tvherror(LS_DVR, "wrong storage path");
@@ -981,8 +977,7 @@ pvr_generate_filename(dvr_entry_t *de, const streaming_start_t *ss)
     if (dir_dosubs) {
       htsstr_substitute(de->de_directory+2, ptmp, sizeof(ptmp), '$', dvr_subs_entry, de, tmp, sizeof(tmp));
     } else {
-      strncpy(ptmp, de->de_directory, sizeof(ptmp)-1);
-      ptmp[sizeof(ptmp)-1] = '\0';
+      strlcpy(ptmp, de->de_directory, sizeof(ptmp));
     }
     s = ptmp;
     while (*s == '/')
index b8f43d2a1306d3ed8ef20f05a9eaf4fd8f8575f7..e46a6a3f23166a14cb29e9fb201eb50725d5a5a5 100644 (file)
@@ -414,7 +414,7 @@ dvr_timerec_entry_class_time_get(void *o, int tm)
   if (tm >= 0)
     snprintf(prop_sbuf, PROP_SBUF_LEN, "%02d:%02d", tm / 60, tm % 60);
   else
-    strncpy(prop_sbuf, N_("Any"), 16);
+    strlcpy(prop_sbuf, N_("Any"), PROP_SBUF_LEN);
   return &prop_sbuf_ptr;
 }
 
index 79fccb9d4a2a7ff7ebf58801c89167432240ec2c..f62868f276a81b6dfe036ebb4faabf64d2de281e 100644 (file)
@@ -78,8 +78,7 @@ dvr_vfs_find1(dvr_vfs_t *old, htsmsg_t *m)
     return dvr_vfs_find(old, &fsid);
   } else if ((s = htsmsg_get_str(m, "fsid0")) != NULL) {
     fsid.fsid = 0;
-    strncpy(fsid.id, s, sizeof(fsid.id)-1);
-    fsid.id[sizeof(fsid.id)-1] = '\0';
+    strlcpy(fsid.id, s, sizeof(fsid.id));
     return dvr_vfs_find(old, &fsid);
   }
   return NULL;
index be1c51b09c44888762b96c8e113bddd917d70b1d..2bda67fd88e0791bc64a763f40b34ec632fb0776 100644 (file)
@@ -521,8 +521,7 @@ epggrab_channel_find_by_id ( const char *id )
   char buf[1024];
   char *mid, *cid;
   epggrab_module_t *mod;
-  strncpy(buf, id, sizeof(buf));
-  buf[sizeof(buf)-1] = '\0';
+  strlcpy(buf, id, sizeof(buf));
   if ((mid = strtok_r(buf, "|", &cid)) && cid)
     if ((mod = epggrab_module_find_by_id(mid)) != NULL)
       return epggrab_channel_find(mod, cid, 0, NULL);
index 3cf36b42f0458e6536c1e5f9e4cd2f8c52592cec..aac9884cfde002d271e8d7253806cb9ca8dceb14 100644 (file)
@@ -652,7 +652,7 @@ epggrab_module_activate_socket ( void *m, int a )
 
     memset(&addr, 0, sizeof(struct sockaddr_un));
     addr.sun_family = AF_UNIX;
-    strncpy(addr.sun_path, mod->path, 100);
+    strlcpy(addr.sun_path, mod->path, sizeof(addr.sun_path));
     if (bind(sock, (struct sockaddr*)&addr,
              sizeof(struct sockaddr_un)) != 0) {
       tvherror(mod->subsys, "%s: failed to bind socket: %s", mod->id, strerror(errno));
index ebc7655da4294b692ba987daef21b194e5e3a672..acf3c08e8d2d3466131e2e8fd9962c9c25caa26e 100644 (file)
@@ -452,8 +452,7 @@ static int _eit_desc_crid
     
       if (crid) {
         if (strstr(buf, "crid://") == buf) {
-          strncpy(crid, buf, clen);
-          crid[clen-1] = '\0';
+          strlcpy(crid, buf, clen);
         } else if (*buf != '/') {
           snprintf(crid, clen, "crid://%s", buf);
         } else {
index 4425615940c24fa12177c1b85620c324588a8055..61176695bee95e7da87d0727a850b7a337370a23 100644 (file)
@@ -40,15 +40,14 @@ static char *get_languages_string(htsmsg_field_t *field)
     if (langlist) {
       htsmsg_field_t *item;
       char langbuf[MAX_TEXT_LEN];
+      size_t l = 0;
       langbuf[0] = '\0';
       HTSMSG_FOREACH(item, langlist) {
         s = htsmsg_field_get_str(item);
-        if (s) {
-          strncat(langbuf, s, sizeof(langbuf) - strlen(langbuf) - 1);
-          strncat(langbuf, "|", sizeof(langbuf) - strlen(langbuf) - 1);
-        }
+        if (s)
+          tvh_strlcatf(langbuf, MAX_TEXT_LEN, l, "%s|", s);
       }
-      if (strlen(langbuf) > 0)
+      if (l > 0)
         return strdup(langbuf);
     }
   }
@@ -143,13 +142,12 @@ void *eit_pattern_apply_list(char *buf, size_t size_buf, const char *text, const
         if (regex_match_substring(&p->compiled, matchno, matchbuf, sizeof(matchbuf)))
           break;
         size_t len = strlen(buf);
-        strncat(buf, matchbuf, size_buf - len - 1);
+        strlcat(buf, matchbuf, size_buf - len);
       }
       rtrim(buf);
       tvhtrace(LS_EPGGRAB,"  pattern \"%s\" matches '%s' from '%s'", p->text, buf, text);
       if (p->filter) {
-        strncpy(textbuf, buf, MAX_TEXT_LEN - 1);
-        textbuf[MAX_TEXT_LEN - 1] = '\0';
+        strlcpy(textbuf, buf, MAX_TEXT_LEN);
         text = textbuf;
         continue;
       }
index 5c7bb28774fa93f2bdf2e0b421d32cd2376a5bf9..10829a48e0ef57926657b12d5efcf85e995f5fe2 100644 (file)
@@ -61,8 +61,7 @@ static time_t _xmltv_str2time(const char *in)
 
   memset(&tm, 0, sizeof(tm));
   tm.tm_mday = 1;               /* Day is one-based not zero-based */
-  strncpy(str, in, sizeof(str));
-  str[sizeof(str)-1] = '\0';
+  strlcpy(str, in, sizeof(str));
 
   /* split tz */
   while (str[sp] && str[sp] != ' ' && str[sp] != '+' && str[sp] != '-')
@@ -376,8 +375,7 @@ static int _xmltv_parse_date_finished
       const size_t len = strlen(str);
       if (len >= 4) {
           char year_buf[32];
-          strncpy(year_buf, str, 4);
-          year_buf[5] = 0;
+          strlcpy(year_buf, str, 5);
           const int64_t year = atoll(year_buf);
           /* Sanity check the year before copying it over. */
           if (year > 1800 && year < 2500) {
index 91940162c40c3482738f589148c93c026e0cd773..f15bdd7978bd6a04472fa72d3f86ddb5c6cfa48e 100644 (file)
@@ -350,8 +350,7 @@ esfilter_class_language_set(void *o, const void *v)
   const char *s = v;
   char n[4];
   int save;
-  strncpy(n, s && s[0] ? lang_code_get(s) : "", 4);
-  n[3] = 0;
+  strlcpy(n, s && s[0] ? lang_code_get(s) : "", 4);
   save = strcmp(esf->esf_language, n);
   strcpy(esf->esf_language, n);
   return save;
@@ -396,8 +395,7 @@ esfilter_class_service_set(void *o, const void *v)
   const char *s = v;
   int save = 0;
   if (strncmp(esf->esf_service, s, UUID_HEX_SIZE)) {
-    strncpy(esf->esf_service, s, UUID_HEX_SIZE);
-    esf->esf_service[UUID_HEX_SIZE-1] = '\0';
+    strlcpy(esf->esf_service, s, UUID_HEX_SIZE);
     save = 1;
   }
   return save;
index 4ebd9f3ea62ad74a53794dfb78351481462d7dea..931357dc2933f606fb7e267eaddd2c6f45a71f54 100644 (file)
@@ -192,8 +192,7 @@ fb_dirent *fb_readdir ( fb_dir *dir )
   fb_dirent *ret = NULL;
   if (dir->type == FB_BUNDLE) {
     if (dir->b.cur) {
-      strncpy(dir->dirent.name, dir->b.cur->name, sizeof(dir->dirent.name)-1);
-      dir->dirent.name[sizeof(dir->dirent.name)-1] = '\0';
+      strlcpy(dir->dirent.name, dir->b.cur->name, sizeof(dir->dirent.name));
       dir->dirent.type = dir->b.cur->type;
       dir->b.cur       = dir->b.cur->next;
       ret              = &dir->dirent;
@@ -261,8 +260,7 @@ int fb_scandir ( const char *path, fb_dirent ***list )
     i = 0;
     while (fb) {
       (*list)[i] = calloc(1, sizeof(fb_dirent));
-      strncpy((*list)[i]->name, fb->name, sizeof((*list)[i]->name));
-      (*list)[i]->name[sizeof((*list)[i]->name)-1] = '\0';
+      strlcpy((*list)[i]->name, fb->name, sizeof((*list)[i]->name));
       fb = fb->next;
       i++;
     }
index 1c1ba0b42847fc72704dd9ddf8d5ef78bf2fa861..26b57f6cd59f8e6701a056cfc4d9bc84f5b9479a 100644 (file)
@@ -267,8 +267,7 @@ http_get_nonce(void)
     mono ^= 0xa1687211885fcd30LL;
     snprintf(stamp, sizeof(stamp), "%"PRId64, mono);
     m = md5sum(stamp, 1);
-    strncpy(n->nonce, m, sizeof(stamp));
-    n->nonce[sizeof(stamp)-1] = '\0';
+    strlcpy(n->nonce, m, sizeof(stamp));
     pthread_mutex_lock(&global_lock);
     if (RB_INSERT_SORTED(&http_nonces, n, link, http_nonce_cmp)) {
       pthread_mutex_unlock(&global_lock);
@@ -289,8 +288,7 @@ http_nonce_exists(const char *nonce)
 
   if (nonce == NULL)
     return 0;
-  strncpy(tmp.nonce, nonce, sizeof(tmp.nonce)-1);
-  tmp.nonce[sizeof(tmp.nonce)-1] = '\0';
+  strlcpy(tmp.nonce, nonce, sizeof(tmp.nonce));
   pthread_mutex_lock(&global_lock);
   n = RB_FIND(&http_nonces, &tmp, link, http_nonce_cmp);
   if (n) {
@@ -1536,10 +1534,8 @@ http_arg_get_remove(struct http_arg_list *list, const char *name)
     if(!strcasecmp(ra->key, name)) {
       TAILQ_REMOVE(list, ra, link);
       empty = ra->val == NULL;
-      if (!empty) {
-        strncpy(buf, ra->val, sizeof(buf)-1);
-        buf[sizeof(buf)-1] = '\0';
-      }
+      if (!empty)
+        strlcpy(buf, ra->val, sizeof(buf));
       free(ra->key);
       free(ra->val);
       free(ra);
index e84718c24ef595f01a8c0cf1d9bd8d2976654e46..e0780520e89b4c1f0fcb1645bbcd30677c6c18d5 100644 (file)
@@ -252,8 +252,7 @@ idnode_get_title(idnode_t *in, const char *lang, char *dst, size_t dstsize)
       return dst;
     }
   }
-  strncpy(dst, idnode_uuid_as_str(in, ubuf), dstsize);
-  dst[dstsize-1] = 0;
+  strlcpy(dst, idnode_uuid_as_str(in, ubuf), dstsize);
   return dst;
 }
 
index cdd0717d4671870b9386fcc06ad15d9f3ad3812c..3ff89560ed1fc95fab47f7792ffcd180e9c7958e 100644 (file)
@@ -657,8 +657,7 @@ imagecache_filename ( int id, char *name, size_t len )
   if (!strncasecmp(i->url, "file://", 7)) {
     fn = tvh_strdupa(i->url + 7);
     http_deescape(fn);
-    strncpy(name, fn, len);
-    name[len-1] = '\0';
+    strlcpy(name, fn, len);
   }
 
   /* Remote file */
index a4f45c72e4405941a8fee7944d51861f597799e1..97d38df16656ae98d9a637d7a40371da9ec63213 100644 (file)
@@ -703,8 +703,7 @@ dvb_freesat_regions
     if (!fr) {
       fr = calloc(1, sizeof(*fr));
       fr->regionid = id;
-      strncpy(fr->name, name, sizeof(fr->name)-1);
-      fr->name[sizeof(fr->name)-1] = '\0';
+      strlcpy(fr->name, name, sizeof(fr->name));
       TAILQ_INIT(&fr->services);
       LIST_INSERT_HEAD(&bi->fregions, fr, link);
     }
@@ -958,8 +957,7 @@ dvb_bskyb_local_channels
           snprintf(buf, sizeof(buf), "Region %d", regionid);
           str = buf;
         }
-        strncpy(fr->name, str, sizeof(fr->name)-1);
-        fr->name[sizeof(fr->name)-1] = '\0';
+        strlcpy(fr->name, str, sizeof(fr->name));
         TAILQ_INIT(&fr->services);
         LIST_INSERT_HEAD(&bi->fregions, fr, link);
       }
@@ -1510,10 +1508,8 @@ dvb_nit_callback
   /* BAT */
   } else if (tableid == 0x4A) {
     tvhdebug(mt->mt_subsys, "%s: bouquet %04X (%d) [%s]", mt->mt_name, nbid, nbid, name);
-    if (bi && *name) {
-      strncpy(bi->name, name, sizeof(bi->name)-1);
-      bi->name[sizeof(bi->name)-1] = '\0';
-    }
+    if (bi && *name)
+      strlcpy(bi->name, name, sizeof(bi->name));
 
   /* NIT */
   } else {
index 8e8e17b19e5ea711ce342d509e87da3956ec7260..a948bc1471043b9f91ae17a68b68438c3850861c 100644 (file)
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <time.h>
 #include "tvhlog.h"
+#include "tvh_string.h"
 #include "en50221.h"
 #include "input/mpegts/dvb.h"
 #include "descrambler/caid.h"
index 0897f3c2445eafb5e84306514ea46a4df7c33ad3..7aefd8f65469db4351c6a9b6b1509e837d8b2ed1 100644 (file)
@@ -292,8 +292,7 @@ iptv_sub_url_encode(iptv_mux_t *im, const char *s, char *tmp, size_t tmplen)
        !strncmp(im->mm_iptv_url, "file://", 7)))
     return s;
   p = url_encode(s);
-  strncpy(tmp, p, tmplen-1);
-  tmp[tmplen-1] = '\0';
+  strlcpy(tmp, p, tmplen);
   free(p);
   return tmp;
 }
index 68e5ef08b32516ffe7cad1744e38b7c86e65dc45..4fa1f275f1c508a76c3baad4ab2c72173221164b 100644 (file)
@@ -335,13 +335,12 @@ iptv_mux_display_name ( mpegts_mux_t *mm, char *buf, size_t len )
 {
   iptv_mux_t *im = (iptv_mux_t*)mm;
   if(im->mm_iptv_muxname) {
-    strncpy(buf, im->mm_iptv_muxname, len);
-    buf[len-1] = '\0';
+    strlcpy(buf, im->mm_iptv_muxname, len);
   } else if(im->mm_iptv_url_sane) {
-    strncpy(buf, im->mm_iptv_url_sane, len);
-    buf[len-1] = '\0';
-  } else
+    strlcpy(buf, im->mm_iptv_url_sane, len);
+  } else {
     *buf = 0;
+  }
 }
 
 /*
index fc525bb3c2a18a218acdf8d009df8bc9e7568306..52f617ee6d63dce729c3f640c4b7f523f0602a07 100644 (file)
@@ -395,10 +395,10 @@ static void
 mpegts_input_display_name ( mpegts_input_t *mi, char *buf, size_t len )
 {
   if (mi->mi_name) {
-    strncpy(buf, mi->mi_name, len - 1);
-    buf[len - 1] = '\0';
-  } else
+    strlcpy(buf, mi->mi_name, len);
+  } else {
     *buf = 0;
+  }
 }
 
 int
index 791c45a1fe5c15b8c122e65148f9100eaad03f0d..6fe5b181612d15c2311bbaffde0cf244db436007 100644 (file)
@@ -342,7 +342,7 @@ static void
 mpegts_network_display_name
   ( mpegts_network_t *mn, char *buf, size_t len )
 {
-  strncpy(buf, tvh_str_default(mn->mn_network_name, "unknown"), len);
+  strlcpy(buf, tvh_str_default(mn->mn_network_name, "unknown"), len);
 }
 
 static bouquet_t *
index 69e9f51b1a245df3b934ec2033559d0ebf39d00d..3c29615fccefad602d64682dc79025f42b8d9105 100644 (file)
@@ -1924,8 +1924,7 @@ new_tune:
             fatal = 1;
             continue;
           } else {
-            strncpy((char *)session, rtsp->hc_rtsp_session ?: "", sizeof(session));
-            session[sizeof(session)-1] = '\0';
+            strlcpy((char *)session, rtsp->hc_rtsp_session ?: "", sizeof(session));
             stream_id = rtsp->hc_rtsp_stream_id;
             tvhdebug(LS_SATIP, "%s #%i - new session %s stream id %li",
                         rtsp->hc_host, lfe->sf_number,
index 631a007b368bc36ef03f43ce80636a40192d5fbe..feeafd0f2fd786e4ce33267fe473793a7ae03523 100644 (file)
@@ -335,8 +335,7 @@ scanfile_create_network
   int opos;
 
   /* Region */
-  strncpy(buf, name, sizeof(buf));
-  buf[sizeof(buf)-1] = '\0';
+  strlcpy(buf, name, sizeof(buf));
   if (!strcmp(type, "dvb-s")) {
     reg = scanfile_region_create(type, "geo", "Geo-synchronous Orbit");
   } else {
index a646db88e95ba6ab906fb0e86bf7d17a65302e01..4df45f279cc917fac58eb3f7d93e466d1c0a0960 100644 (file)
@@ -107,10 +107,8 @@ intlconv_charset_id( const char *charset,
     snprintf(buf, sizeof(buf), "%s%sTRANSLIT", charset, delim);
   else if (ignore_bad_chars)
     snprintf(buf, sizeof(buf), "%s%sIGNORE", charset, delim);
-  else {
-    strncpy(buf, charset, sizeof(buf));
-    buf[sizeof(buf)-1] = '\0';
-  }
+  else
+    strlcpy(buf, charset, sizeof(buf));
   return buf;
 }
 
@@ -125,8 +123,7 @@ intlconv_utf8( char *dst, size_t dst_size,
   ssize_t res;
 
   if (dst_charset_id == NULL) {
-    strncpy(dst, src_utf8, dst_size);
-    dst[dst_size - 1] = '\0';
+    strlcpy(dst, src_utf8, dst_size);
     return strlen(dst);
   }
   templ.ic_charset_id = (char *)dst_charset_id;
@@ -214,8 +211,7 @@ intlconv_to_utf8( char *dst, size_t dst_size,
   ssize_t res;
 
   if (src_charset_id == NULL) {
-    strncpy(dst, src, dst_size);
-    dst[dst_size - 1] = '\0';
+    strlcpy(dst, src, dst_size);
     return strlen(dst);
   }
   templ.ic_charset_id = (char *)src_charset_id;
index 09ef3b3b55fdbfc43adbc7733c4fde48609fb3c0..bbf3b5b014747c8e75ab8c1de5f0dd96f514c93e 100644 (file)
@@ -97,7 +97,7 @@ lang_str_ele_t *lang_str_get2
   /* Check config/requested langs */
   if ((langs = lang_code_split(lang)) != NULL) {
     for (i = 0; i < langs->codeslen; i++) {
-      strncpy(skel.lang, langs->codes[i]->code2b, sizeof(skel.lang));
+      strlcpy(skel.lang, langs->codes[i]->code2b, sizeof(skel.lang));
       if ((e = RB_FIND(ls, &skel, link, _lang_cmp)))
         break;
     }
@@ -130,7 +130,7 @@ static int _lang_str_add
   /* Create */
   if (!e) {
     e = malloc(sizeof(*e) + strlen(str) + 1);
-    strncpy(e->lang, lang, sizeof(e->lang));
+    strlcpy(e->lang, lang, sizeof(e->lang));
     strcpy(e->str, str);
     RB_INSERT_SORTED(ls, e, link, _lang_cmp);
     save = 1;
index 274361e25cdcfef21c1d4f48ad5e1015543af892..98b0ac8b4dcf0db544e0e68bedbee3720a763188 100644 (file)
@@ -54,8 +54,8 @@ rtsp_send_ext( http_client_t *hc, http_cmd_t cmd,
       hdr = &h;
       http_arg_init(&h);
     }
-    strncpy(buf_body, body, sizeof(buf_body));
-    strncat(buf_body, "\r\n", 2);
+    strlcpy(buf_body, body, sizeof(buf_body));
+    strlcat(buf_body, "\r\n", 2);
     snprintf(buf2, sizeof(buf2), "%"PRIu64, (uint64_t)(size + 2));
     http_arg_set(hdr, "Content-Length", buf2);
   }
index a062d5f987265f5bad3d524834c993819bce1f98..9364baef3424a70db9f0f41214e05aef8857e65a 100644 (file)
@@ -670,8 +670,7 @@ satip_rtcp_fec(int fec)
   s = dvb_fec2str(fec);
   if (s == NULL)
     return "";
-  strncpy(buf, s, sizeof(buf));
-  buf[sizeof(buf)-1] = '\0';
+  strlcpy(buf, s, sizeof(buf));
   p = strchr(buf, '/');
   while (p && *p) {
     *p = *(p+1);
index 89e40f4c1be5d0ae1cb232596f5455930e6ee48b..cbde6f6519ba3430e4ec8e7807e5e2ac68ec1b4a 100644 (file)
@@ -1596,8 +1596,7 @@ rtsp_process_teardown(http_connection_t *hc)
     pthread_mutex_unlock(&rtsp_lock);
     http_error(hc, !rs ? HTTP_STATUS_BAD_SESSION : HTTP_STATUS_NOT_FOUND);
   } else {
-    strncpy(session, rs->session, sizeof(session));
-    session[sizeof(session)-1] = '\0';
+    strlcpy(session, rs->session, sizeof(session));
     rtsp_close_session(rs);
     rtsp_free_session(rs);
     pthread_mutex_unlock(&rtsp_lock);
index b3999d7c18011342ee7fb319eb32a9873a989ac9..090c8356b4dadc83c30755def808f35b7e747152 100644 (file)
@@ -1402,8 +1402,7 @@ service_instance_add(service_instance_list_t *sil,
   }
   si->si_weight = weight;
   si->si_prio   = prio;
-  strncpy(si->si_source, source ?: "<unknown>", sizeof(si->si_source));
-  si->si_source[sizeof(si->si_source)-1] = '\0';
+  strlcpy(si->si_source, source ?: "<unknown>", sizeof(si->si_source));
   TAILQ_INSERT_SORTED(sil, si, si_link, si_cmp);
   return si;
 }
@@ -1758,7 +1757,7 @@ void service_load ( service_t *t, htsmsg_t *c )
       st = elementary_stream_create(&t->s_components, pid, type);
 
       if((v = htsmsg_get_str(c, "language")) != NULL)
-        strncpy(st->es_lang, lang_code_get(v), 3);
+        strlcpy(st->es_lang, lang_code_get(v), 4);
 
       if (SCT_ISAUDIO(type)) {
         if(!htsmsg_get_u32(c, "audio_type", &u32))
index 0f1481ff1770275215a9b24a12ab47b03dbb2408..8e3c9efe1d76a374f7ff97a532963860dfc02fea 100644 (file)
@@ -101,7 +101,7 @@ _hts_settings_buildpath
   if (*tmp != '/' && prefix)
     snprintf(dst, dstsize, "%s/%s", prefix, tmp);
   else
-    strncpy(dst, tmp, dstsize);
+    strlcpy(dst, tmp, dstsize);
 
   while(*n) {
     if(*n == ':' || *n == '?' || *n == '*' || *n > 127 || *n < 32)
index 913132178ef69a47930edb9f4090a4f3a8510e4d..78fa2cba266a20e28153c60d9ec5572d59c3ae5a 100644 (file)
@@ -201,8 +201,7 @@ find_exec ( const char *name, char *out, size_t len )
   if (name[0] == '/') {
     if (lstat(name, &st)) return 0;
     if (!S_ISREG(st.st_mode) || !(st.st_mode & S_IEXEC)) return 0;
-    strncpy(out, name, len);
-    out[len-1] = '\0';
+    strlcpy(out, name, len);
     return 1;
   }
   if (!(path = getenv("PATH"))) return 0;
@@ -215,8 +214,7 @@ find_exec ( const char *name, char *out, size_t len )
         snprintf(bin, sizeof(bin), "%s/%s", tmp, de->d_name);
         if (lstat(bin, &st)) continue;
         if (!S_ISREG(st.st_mode) || !(st.st_mode & S_IEXEC)) continue;
-        strncpy(out, bin, len);
-        out[len-1] = '\0';
+        strlcpy(out, bin, len);
         ret = 1;
         break;
       }
index 9b865eb292cbb4d4f670ba8d913a053096acc73f..5349784a48b258f19913d22a91d498125a48d0e7 100644 (file)
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -481,7 +481,7 @@ tcp_get_str_from_ip(const struct sockaddr_storage *sa, char *dst, size_t maxlen)
       inet_ntop(AF_INET6, &(((struct sockaddr_in6*)sa)->sin6_addr), dst, maxlen);
       break;
     default:
-      strncpy(dst, "Unknown AF", maxlen);
+      strlcpy(dst, "Unknown AF", maxlen);
       return NULL;
   }
 
index 76435f1af6ad1f72524b95aed955c87b23487c04..70599ccf1eeb8abe2a8dac3f076d1ef5f98056ee 100644 (file)
@@ -22,6 +22,7 @@
 #include <pthread.h>
 #include "pthread.h"
 #include "tvh_locale.h"
+#include "tvh_string.h"
 #include "redblack.h"
 
 struct tvh_locale {
@@ -198,8 +199,7 @@ static void tvh_gettext_default_init(void)
   if (p == NULL)
     p = (char *)"en_US";
 
-  strncpy(dflt, p, sizeof(dflt)-1);
-  dflt[sizeof(dflt)-1] = '\0';
+  strlcpy(dflt, p, sizeof(dflt));
   for (p = dflt; *p && *p != '.'; p++);
   if (*p == '.') *p = '\0';
 
index 27367d2346436bd8491298dda2839910bcd01d59..3ff3decc0844e451279e68a8a51925c96b466f42 100644 (file)
@@ -36,6 +36,7 @@
 #include <sys/syscall.h>
 #endif
 #include "queue.h"
+#include "tvh_string.h"
 #include "hts_strtab.h"
 #include "htsmsg.h"
 #include "tprofile.h"
@@ -237,26 +238,11 @@ void tasklet_disarm(tasklet_t *gti);
 
 int tvh_kill_to_sig(int tvh_kill);
 
-static inline unsigned int tvh_strhash(const char *s, unsigned int mod)
-{
-  unsigned int v = 5381;
-  while(*s)
-    v += (v << 5) + v + *s++;
-  return v % mod;
-}
-
 #define MIN(a,b) ((a) < (b) ? (a) : (b))
 #define MAX(a,b) ((a) > (b) ? (a) : (b))
 #define MINMAX(a,mi,ma) MAX(mi, MIN(ma, a))
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 
-static inline const char *tvh_str_default(const char *s, const char *dflt)
-{
-  return s && s[0] ? s : dflt;
-}
-void tvh_str_set(char **strp, const char *src);
-int tvh_str_update(char **strp, const char *src);
-
 int sri_to_rate(int sri);
 int rate_to_sri(int rate);
 
@@ -269,31 +255,12 @@ extern void scopedunlock(pthread_mutex_t **mtxp);
 
 #define scopedgloballock() scopedlock(&global_lock)
 
-#define tvh_strdupa(n) \
-  ({ int tvh_l = strlen(n); \
-     char *tvh_b = alloca(tvh_l + 1); \
-     memcpy(tvh_b, n, tvh_l + 1); })
-
-static inline const char *tvh_strbegins(const char *s1, const char *s2)
-{
-  while(*s2)
-    if(*s1++ != *s2++)
-      return NULL;
-  return s1;
-}
-
 typedef struct th_pipe
 {
   int rd;
   int wr;
 } th_pipe_t;
 
-static inline void mystrset(char **p, const char *s)
-{
-  free(*p);
-  *p = s ? strdup(s) : NULL;
-}
-
 void doexit(int x);
 
 int tvhthread_create
@@ -340,10 +307,6 @@ char *base64_encode(char *out, int out_size, const uint8_t *in, int in_size);
 /* Calculate the output size needed to base64-encode x bytes. */
 #define BASE64_SIZE(x) (((x)+2) / 3 * 4 + 1)
 
-int put_utf8(char *out, int c);
-
-char *utf8_lowercase_inplace(char *s);
-
 static inline int64_t ts_rescale(int64_t ts, int tb)
 {
   //  return (ts * tb + (tb / 2)) / 90000LL;
index 71df76138778bf6ce1909177b2ba43f52f0fa4ad..1c97ffe5537918993336214ee47869cc84046c11 100644 (file)
@@ -355,8 +355,7 @@ tvhlog_thread ( void *p )
     /* Copy options and path */
     if (!fp) {
       if (tvhlog_path) {
-        strncpy(buf, tvhlog_path, sizeof(buf)-1);
-        buf[sizeof(buf)-1] = '\0';
+        strlcpy(buf, tvhlog_path, sizeof(buf));
         path = buf;
       } else {
         path = NULL;
index 97a4310ade9a7647e8762bea6551a34be014e50a..b12184a92540112ac6d13df9fe8589226354c134 100644 (file)
@@ -243,12 +243,6 @@ static inline void tvhtrace_no_warnings(const char *fmt, ...) { (void)fmt; }
   abort(); \
 } while (0)
 
-#define tvh_strlen(s) ((s) ? strlen(s) : 0)
-
-#define tvh_strlcatf(buf, size, ptr, fmt...) \
-  do { int __r = snprintf((buf) + ptr, (size) - ptr, fmt); \
-       ptr = __r >= (size) - ptr ? (size) - 1 : ptr + __r; } while (0)
-
 void tvhlog_backtrace_printf(const char *fmt, ...);
 
 #endif /* __TVH_LOGGING_H__ */
index 74273ddae9af674f21c5c17fc8fc8569db4f3cce..192877069806a99bb798f8f43d1ff6f5329e6bc0 100644 (file)
--- a/src/udp.c
+++ b/src/udp.c
@@ -129,7 +129,7 @@ udp_get_ifaddr( int fd, const char *ifname, struct in_addr *addr )
     return -1;
   
   memset(&ifreq, 0, sizeof(ifreq));
-  strncpy(ifreq.ifr_name, ifname, IFNAMSIZ-1);
+  strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
   
   if (ioctl(fd, SIOCGIFADDR, &ifreq) < 0)
     return -1;
index 94441fdcd2e364df8d7bc3d4dfa2cfcd9e460b68..77a1eaaf84c0cd7840c9c58add20b49e322771b0 100644 (file)
--- a/src/url.c
+++ b/src/url.c
@@ -129,7 +129,7 @@ urlparse ( const char *str, url_t *url )
     size_t len = x.afterLast - x.first;\
     if (len > sizeof(y) - 1) \
     { y[0] = '\0'; s = strndup(x.first, len); } else \
-    { s = NULL; strncpy(y, x.first, len); y[len] = '\0'; }\
+    { s = NULL; strlcpy(y, x.first, len + 1); }\
   } else {\
     s = NULL;\
     y[0] = '\0';\
index 9eb6e6db8f17a41bda7c6116e7640676a0241b23..bf3e86512a621eedae7b41dd79d30c39b7eb1cf2 100644 (file)
@@ -254,7 +254,7 @@ wizard_page_t *wizard_hello(const char *lang)
   page->aux = w = calloc(1, sizeof(wizard_hello_t));
 
   if (config.language_ui)
-    strncpy(w->ui_lang, config.language_ui, sizeof(w->ui_lang)-1);
+    strlcpy(w->ui_lang, config.language_ui, sizeof(w->ui_lang));
 
   m = htsmsg_csv_2_list(config.language, ',');
   f = m ? HTSMSG_FIRST(m) : NULL;
@@ -262,9 +262,9 @@ wizard_page_t *wizard_hello(const char *lang)
     s = htsmsg_field_get_string(f);
     if (s == NULL) break;
     switch (idx) {
-    case 0: strncpy(w->epg_lang1, s, sizeof(w->epg_lang1) - 1); break;
-    case 1: strncpy(w->epg_lang2, s, sizeof(w->epg_lang2) - 1); break;
-    case 2: strncpy(w->epg_lang3, s, sizeof(w->epg_lang3) - 1); break;
+    case 0: strlcpy(w->epg_lang1, s, sizeof(w->epg_lang1)); break;
+    case 1: strlcpy(w->epg_lang2, s, sizeof(w->epg_lang2)); break;
+    case 2: strlcpy(w->epg_lang3, s, sizeof(w->epg_lang3)); break;
     }
     f = HTSMSG_NEXT(f);
   }
index 1b1671f68f70185c0b4475d59cae1a18a3f6c2fb..ba15e307604a71809721106b7faafbf943778a34 100644 (file)
@@ -197,9 +197,8 @@ tvhthread_create
     pthread_attr_setstacksize(&_attr, 2*1024*1024);
     attr = &_attr;
   }
-  strncpy(ts->name, "tvh:", 4);
-  strncpy(ts->name+4, name, sizeof(ts->name)-4);
-  ts->name[sizeof(ts->name)-1] = '\0';
+  strlcpy(ts->name, "tvh:", 5);
+  strlcpy(ts->name+4, name, sizeof(ts->name)-4);
   ts->run  = start_routine;
   ts->arg  = arg;
   r = pthread_create(thread, attr, thread_wrapper, ts);
@@ -605,8 +604,7 @@ int regex_match_substring(tvh_regex_t *regex, unsigned number, char *buf, size_t
     ssize_t size = regex->re_posix_match[number].rm_eo - regex->re_posix_match[number].rm_so;
     if (size < 0 || size > (size_buf - 1))
       return -1;
-    memcpy(buf, regex->re_posix_text + regex->re_posix_match[number].rm_so, size);
-    buf[size] = '\0';
+    strlcpy(buf, regex->re_posix_text + regex->re_posix_match[number].rm_so, size);
     return 0;
 #if ENABLE_PCRE || ENABLE_PCRE2
   } else {