]> 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>
Mon, 2 Jul 2018 13:27:51 +0000 (15:27 +0200)
38 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/xmltv.c
src/esfilter.c
src/filebundle.c
src/http.c
src/imagecache.c
src/input/mpegts/dvb_psi.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/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/udp.c
src/url.c
src/wizard.c
src/wrappers.c

index 8c4353d12f5cf73a20e303bc9eb9a4ad45a24a39..b7bf11a1c2e493a396bc42c9fac088260c3dc0f1 100644 (file)
@@ -892,8 +892,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 cbff13baeb767576b213314c1bb91b6c769e5065..7175ed876afb91efe620c8de9d82855532c9c158 100644 (file)
@@ -937,8 +937,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 2376ed527f02bbb4b42cb664e4bc8d628bdc5db5..ae57ded53260a15420be14f955ab5d18c0c0df1f 100644 (file)
@@ -406,10 +406,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 239d3b7a7d738fc23c729bc8a86a3a7ef2caf6a4..a0d3a76c1a5745ee587122e4358959e47b01658e 100644 (file)
@@ -623,7 +623,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 d3766e4b966ed7354e751a970482d4a65f07377f..2cdea043a502bf3a4a3a3ad171c39e8133cccf30 100644 (file)
@@ -3083,8 +3083,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;
 }
 
@@ -3092,8 +3091,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 d4e763d1c80d8cf467efadb220decaeef667eb2c..38de733115a61eed686e62d02c11b5e55c40b7dc 100644 (file)
@@ -320,8 +320,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);
 }
@@ -562,8 +561,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);
 }
 
@@ -598,8 +596,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);
 }
 
@@ -703,8 +700,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");
@@ -736,8 +732,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 0f7589ed83c4986fb5a0fe07265ed0fd35b0fe58..bfb05d0c2b0c257442dd8d705d27cc71d769faea 100644 (file)
@@ -402,7 +402,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 c2e1d5bfbee8e3f014db7cdaf8e7ca41fd4c7e21..0f5352a81513fd799e85236917b988ff4fdd6944 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 453fc6f844d7d12625a480e58072bd97c3843a25..9eeae31d7181bf7c9cf0c989e5a835ce629e388f 100644 (file)
@@ -528,7 +528,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(mod->sock, (struct sockaddr*)&addr,
              sizeof(struct sockaddr_un)) != 0 ) {
       tvherror(mod->subsys, "%s: failed to bind socket", mod->id);
index 11e7349b61e4a23262371c454e79a1bfbaca8d75..1bbdd1af9bee0489e5f6c0fa3b2f6d1d3d9a7848 100644 (file)
@@ -386,9 +386,8 @@ static int _eit_desc_crid
     
       if (crid) {
         if (strstr(buf, "crid://") == buf) {
-          strncpy(crid, buf, clen);
-          crid[clen-1] = '\0';
-        } else if ( *buf != '/' ) {
+          strlcpy(crid, buf, clen);
+        } else if (*buf != '/') {
           snprintf(crid, clen, "crid://%s", buf);
         } else {
           const char *defauth = svc->s_dvb_cridauth;
index f67123bef3536022abed2f8238939e3b198b0487..61cb6333a2ac25837801920bee80845e67b46445 100644 (file)
@@ -59,8 +59,8 @@ static time_t _xmltv_str2time(const char *in)
   char str[32];
 
   memset(&tm, 0, sizeof(tm));
-  strncpy(str, in, sizeof(str));
-  str[sizeof(str)-1] = '\0';
+  tm.tm_mday = 1;               /* Day is one-based not zero-based */
+  strlcpy(str, in, sizeof(str));
 
   /* split tz */
   while (str[sp] && str[sp] != ' ' && str[sp] != '+' && str[sp] != '-')
index 9071bd7fddb7d2ca05314d8887cd60716073a0ae..b3920249a9b5ffce04e6df49d531f8d98a02a9dd 100644 (file)
@@ -356,8 +356,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;
@@ -402,8 +401,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 96053ee16f540a6037c46cdc5c00942ee1906b75..1a0081f79c0347d62f1a9b33bb8a41ef20a251de 100644 (file)
@@ -265,8 +265,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);
@@ -287,8 +286,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) {
@@ -1406,10 +1404,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 d12149c550c5fa73d697dbf829964adaf96b0bc8..33212694ce3bdce71f62f859fe0c93d6cc0a8cde 100644 (file)
@@ -656,8 +656,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 81cdebd2d4e0d40dad3a610106800c0da1cb1747..aa15ae946c9eaed3074f8c311ce279bc01659134 100644 (file)
@@ -663,8 +663,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);
     }
@@ -918,8 +917,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);
       }
@@ -1556,10 +1554,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 07e94dc00b8a869dee60fe4f05d161c012a7bf3c..6b0f6bf665ab976bc8d26bb5c5736253b19783f9 100644 (file)
@@ -281,8 +281,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 254b916121f47263d64088a253d4b66ddec61f4a..f83e13e9a1c7e0c57faaeb12db613793b2b16476 100644 (file)
@@ -328,13 +328,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 42b947865889dcad9e4e96fb25aa99ab6ed35c3b..e5e439e607494dc9c1e28567bc0135c1b432b94a 100644 (file)
@@ -403,10 +403,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 da613d62bed731b818511c3bdf908ff234735033..96452faa0ceeec1cd9c8643cba1aeb65fa604adc 100644 (file)
@@ -317,7 +317,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 htsmsg_t *
index 23ef25c8a5ba04ff1ea9ea3f22a7a13a3711b92e..1d4698f153b6fbf7d1b1e4cdea8713e13437b470 100644 (file)
@@ -1762,8 +1762,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 3fe3eb683587911ad16eece01388fedf26468d34..3f230f34fc777026a8591f03bacfc0ac2ea35cef 100644 (file)
@@ -89,10 +89,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;
 }
 
@@ -107,8 +105,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;
@@ -196,8 +193,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 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 8e50157476b9b9cb38988829d7db9b0b016064ba..57a999ad67c55274fe43ec7f76f9a61795b1bc41 100644 (file)
@@ -1519,8 +1519,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 967b355bad5e47318b45629b83ad856408b0198d..db7be480787c071f21085140768b25783b87ded5 100644 (file)
@@ -1847,8 +1847,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;
 }
@@ -2197,7 +2196,7 @@ void service_load ( service_t *t, htsmsg_t *c )
       st = service_stream_create(t, 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 c1609b7b3acd23108e8a754c0db32818c46eff1f..115a693550f40cca79545d55923a4de4233bffd1 100644 (file)
@@ -100,7 +100,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 24a3ea9315817151bfa161a6047d3680c5fa6779..6157af40a85ef5b3330ebef4696b3578711ad5ac 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 71d2e5fa1e1f528b75b888f136261d4a8331a996..c3a5d2385c7ec1544884872a5b52277a9f2c6b2b 100644 (file)
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -441,7 +441,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 d87bead40f3fcd41b96005e86cdc8c868c31f60d..e9ec6bbe3dfdee6d82109224255663b53ea824fb 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 "tvhlog.h"
@@ -635,26 +636,11 @@ const char *streaming_component_type2txt(streaming_component_type_t s);
 streaming_component_type_t streaming_component_txt2type(const char *s);
 const char *streaming_component_audio_type2desc(int audio_type);
 
-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);
 
@@ -669,37 +655,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); })
-
-#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)
-
-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
@@ -744,10 +705,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 66941ea6839bb494dba84618b4f55f8511e9537b..26d7e3733e08a8c6f573febd23eb1e01677433a6 100644 (file)
@@ -347,7 +347,7 @@ tvhlog_thread ( void *p )
     /* Copy options and path */
     if (!fp) {
       if (tvhlog_path) {
-        strncpy(buf, tvhlog_path, sizeof(buf));
+        strlcpy(buf, tvhlog_path, sizeof(buf));
         path = buf;
       } else {
         path = NULL;
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 0d54ced695e883e1f5b186a4bb67727e9c607691..0f5a42b988bd41138c54dedbd216af8976f77e27 100644 (file)
--- a/src/url.c
+++ b/src/url.c
@@ -92,7 +92,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 9ca296937ecec83d83783c4aeb523cfa75fbcee7..5c7441338b344f7e9cefd898cf91d18bea0b250c 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 92380388bac4f73eae2a7d4d25821cab32060f6f..93ceaaa82308a77dbb8801635bce4345f306d7c5 100644 (file)
@@ -175,9 +175,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);