From: Jaroslav Kysela Date: Wed, 20 Jun 2018 12:23:43 +0000 (+0200) Subject: introduce strlcpy() and strlcat() functions and use them X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bff422214b3675154ddcaf924e26ac67f3e29029;p=thirdparty%2Ftvheadend.git introduce strlcpy() and strlcat() functions and use them --- diff --git a/src/access.c b/src/access.c index f652752af..bf93919c5 100644 --- a/src/access.c +++ b/src/access.c @@ -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) { diff --git a/src/channels.c b/src/channels.c index 9cf5ad43c..7e0c24bcc 100644 --- a/src/channels.c +++ b/src/channels.c @@ -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; diff --git a/src/descrambler/descrambler.c b/src/descrambler/descrambler.c index 15b54a05e..f6ea0fe6a 100644 --- a/src/descrambler/descrambler.c +++ b/src/descrambler/descrambler.c @@ -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); diff --git a/src/dvr/dvr_autorec.c b/src/dvr/dvr_autorec.c index 1e1f4c915..17413b422 100644 --- a/src/dvr/dvr_autorec.c +++ b/src/dvr/dvr_autorec.c @@ -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; } diff --git a/src/dvr/dvr_db.c b/src/dvr/dvr_db.c index a342b0cd5..e1a087128 100644 --- a/src/dvr/dvr_db.c +++ b/src/dvr/dvr_db.c @@ -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; } diff --git a/src/dvr/dvr_rec.c b/src/dvr/dvr_rec.c index 677c52124..3a539faa7 100644 --- a/src/dvr/dvr_rec.c +++ b/src/dvr/dvr_rec.c @@ -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 == '/') diff --git a/src/dvr/dvr_timerec.c b/src/dvr/dvr_timerec.c index b8f43d2a1..e46a6a3f2 100644 --- a/src/dvr/dvr_timerec.c +++ b/src/dvr/dvr_timerec.c @@ -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; } diff --git a/src/dvr/dvr_vfsmgr.c b/src/dvr/dvr_vfsmgr.c index 79fccb9d4..f62868f27 100644 --- a/src/dvr/dvr_vfsmgr.c +++ b/src/dvr/dvr_vfsmgr.c @@ -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; diff --git a/src/epggrab/channel.c b/src/epggrab/channel.c index be1c51b09..2bda67fd8 100644 --- a/src/epggrab/channel.c +++ b/src/epggrab/channel.c @@ -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); diff --git a/src/epggrab/module.c b/src/epggrab/module.c index 3cf36b42f..aac9884cf 100644 --- a/src/epggrab/module.c +++ b/src/epggrab/module.c @@ -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)); diff --git a/src/epggrab/module/eit.c b/src/epggrab/module/eit.c index ebc7655da..acf3c08e8 100644 --- a/src/epggrab/module/eit.c +++ b/src/epggrab/module/eit.c @@ -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 { diff --git a/src/epggrab/module/eitpatternlist.c b/src/epggrab/module/eitpatternlist.c index 442561594..61176695b 100644 --- a/src/epggrab/module/eitpatternlist.c +++ b/src/epggrab/module/eitpatternlist.c @@ -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; } diff --git a/src/epggrab/module/xmltv.c b/src/epggrab/module/xmltv.c index 5c7bb2877..10829a48e 100644 --- a/src/epggrab/module/xmltv.c +++ b/src/epggrab/module/xmltv.c @@ -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) { diff --git a/src/esfilter.c b/src/esfilter.c index 91940162c..f15bdd797 100644 --- a/src/esfilter.c +++ b/src/esfilter.c @@ -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; diff --git a/src/filebundle.c b/src/filebundle.c index 4ebd9f3ea..931357dc2 100644 --- a/src/filebundle.c +++ b/src/filebundle.c @@ -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++; } diff --git a/src/http.c b/src/http.c index 1c1ba0b42..26b57f6cd 100644 --- a/src/http.c +++ b/src/http.c @@ -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); diff --git a/src/idnode.c b/src/idnode.c index e84718c24..e0780520e 100644 --- a/src/idnode.c +++ b/src/idnode.c @@ -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; } diff --git a/src/imagecache.c b/src/imagecache.c index cdd0717d4..3ff89560e 100644 --- a/src/imagecache.c +++ b/src/imagecache.c @@ -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 */ diff --git a/src/input/mpegts/dvb_psi.c b/src/input/mpegts/dvb_psi.c index a4f45c72e..97d38df16 100644 --- a/src/input/mpegts/dvb_psi.c +++ b/src/input/mpegts/dvb_psi.c @@ -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 { diff --git a/src/input/mpegts/en50221/en50221_apps.c b/src/input/mpegts/en50221/en50221_apps.c index 8e8e17b19..a948bc147 100644 --- a/src/input/mpegts/en50221/en50221_apps.c +++ b/src/input/mpegts/en50221/en50221_apps.c @@ -21,6 +21,7 @@ #include #include #include "tvhlog.h" +#include "tvh_string.h" #include "en50221.h" #include "input/mpegts/dvb.h" #include "descrambler/caid.h" diff --git a/src/input/mpegts/iptv/iptv.c b/src/input/mpegts/iptv/iptv.c index 0897f3c24..7aefd8f65 100644 --- a/src/input/mpegts/iptv/iptv.c +++ b/src/input/mpegts/iptv/iptv.c @@ -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; } diff --git a/src/input/mpegts/iptv/iptv_mux.c b/src/input/mpegts/iptv/iptv_mux.c index 68e5ef08b..4fa1f275f 100644 --- a/src/input/mpegts/iptv/iptv_mux.c +++ b/src/input/mpegts/iptv/iptv_mux.c @@ -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; + } } /* diff --git a/src/input/mpegts/mpegts_input.c b/src/input/mpegts/mpegts_input.c index fc525bb3c..52f617ee6 100644 --- a/src/input/mpegts/mpegts_input.c +++ b/src/input/mpegts/mpegts_input.c @@ -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 diff --git a/src/input/mpegts/mpegts_network.c b/src/input/mpegts/mpegts_network.c index 791c45a1f..6fe5b1816 100644 --- a/src/input/mpegts/mpegts_network.c +++ b/src/input/mpegts/mpegts_network.c @@ -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 * diff --git a/src/input/mpegts/satip/satip_frontend.c b/src/input/mpegts/satip/satip_frontend.c index 69e9f51b1..3c29615fc 100644 --- a/src/input/mpegts/satip/satip_frontend.c +++ b/src/input/mpegts/satip/satip_frontend.c @@ -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, diff --git a/src/input/mpegts/scanfile.c b/src/input/mpegts/scanfile.c index 631a007b3..feeafd0f2 100644 --- a/src/input/mpegts/scanfile.c +++ b/src/input/mpegts/scanfile.c @@ -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 { diff --git a/src/intlconv.c b/src/intlconv.c index a646db88e..4df45f279 100644 --- a/src/intlconv.c +++ b/src/intlconv.c @@ -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; diff --git a/src/lang_str.c b/src/lang_str.c index 09ef3b3b5..bbf3b5b01 100644 --- a/src/lang_str.c +++ b/src/lang_str.c @@ -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; diff --git a/src/rtsp.c b/src/rtsp.c index 274361e25..98b0ac8b4 100644 --- a/src/rtsp.c +++ b/src/rtsp.c @@ -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); } diff --git a/src/satip/rtp.c b/src/satip/rtp.c index a062d5f98..9364baef3 100644 --- a/src/satip/rtp.c +++ b/src/satip/rtp.c @@ -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); diff --git a/src/satip/rtsp.c b/src/satip/rtsp.c index 89e40f4c1..cbde6f651 100644 --- a/src/satip/rtsp.c +++ b/src/satip/rtsp.c @@ -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); diff --git a/src/service.c b/src/service.c index b3999d7c1..090c8356b 100644 --- a/src/service.c +++ b/src/service.c @@ -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 ?: "", sizeof(si->si_source)); - si->si_source[sizeof(si->si_source)-1] = '\0'; + strlcpy(si->si_source, source ?: "", 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)) diff --git a/src/settings.c b/src/settings.c index 0f1481ff1..8e3c9efe1 100644 --- a/src/settings.c +++ b/src/settings.c @@ -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) diff --git a/src/spawn.c b/src/spawn.c index 913132178..78fa2cba2 100644 --- a/src/spawn.c +++ b/src/spawn.c @@ -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; } diff --git a/src/tcp.c b/src/tcp.c index 9b865eb29..5349784a4 100644 --- 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; } diff --git a/src/tvh_locale.c b/src/tvh_locale.c index 76435f1af..70599ccf1 100644 --- a/src/tvh_locale.c +++ b/src/tvh_locale.c @@ -22,6 +22,7 @@ #include #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'; diff --git a/src/tvheadend.h b/src/tvheadend.h index 27367d234..3ff3decc0 100644 --- a/src/tvheadend.h +++ b/src/tvheadend.h @@ -36,6 +36,7 @@ #include #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; diff --git a/src/tvhlog.c b/src/tvhlog.c index 71df76138..1c97ffe55 100644 --- a/src/tvhlog.c +++ b/src/tvhlog.c @@ -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; diff --git a/src/tvhlog.h b/src/tvhlog.h index 97a4310ad..b12184a92 100644 --- a/src/tvhlog.h +++ b/src/tvhlog.h @@ -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__ */ diff --git a/src/udp.c b/src/udp.c index 74273ddae..192877069 100644 --- 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; diff --git a/src/url.c b/src/url.c index 94441fdcd..77a1eaaf8 100644 --- 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';\ diff --git a/src/wizard.c b/src/wizard.c index 9eb6e6db8..bf3e86512 100644 --- a/src/wizard.c +++ b/src/wizard.c @@ -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); } diff --git a/src/wrappers.c b/src/wrappers.c index 1b1671f68..ba15e3076 100644 --- a/src/wrappers.c +++ b/src/wrappers.c @@ -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 {