From: Jaroslav Kysela Date: Wed, 14 Dec 2016 10:21:47 +0000 (+0100) Subject: cwc: trim username and hostname string on load (config/webui), fixes #4135 X-Git-Tag: v4.2.1~165 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f6f9b8add7be6e9a324d805925e263a4eb75ae3f;p=thirdparty%2Ftvheadend.git cwc: trim username and hostname string on load (config/webui), fixes #4135 --- diff --git a/src/descrambler/cwc.c b/src/descrambler/cwc.c index 6e1bb1721..35d5fc9e5 100644 --- a/src/descrambler/cwc.c +++ b/src/descrambler/cwc.c @@ -1833,6 +1833,7 @@ const idclass_t caclient_cwc_class = .name = N_("Username"), .desc = N_("Login username."), .off = offsetof(cwc_t, cwc_username), + .opts = PO_TRIM, }, { .type = PT_STR, @@ -1849,6 +1850,7 @@ const idclass_t caclient_cwc_class = .desc = N_("Hostname (or IP) of the server."), .off = offsetof(cwc_t, cwc_hostname), .def.s = "localhost", + .opts = PO_TRIM, }, { .type = PT_INT, diff --git a/src/prop.c b/src/prop.c index bc6a2795d..fb33d128a 100644 --- a/src/prop.c +++ b/src/prop.c @@ -75,18 +75,19 @@ prop_write_values htsmsg_field_t *f; const property_t *p; void *cur; - const void *new; + const void *snew; + void *dnew; double dbl; - int i; + int i; int64_t s64; - uint32_t u32; + uint32_t u32, opts; uint16_t u16; time_t tm; #define PROP_UPDATE(v, t)\ - new = &v;\ - if (!p->set && (*((t*)cur) != *((t*)new))) {\ + snew = &v;\ + if (!p->set && (*((t*)cur) != *((t*)snew))) {\ save = 1;\ - *((t*)cur) = *((t*)new);\ + *((t*)cur) = *((t*)snew);\ } (void)0 if (!pl) return 0; @@ -99,8 +100,8 @@ prop_write_values if (!f) continue; /* Ignore */ - u32 = p->get_opts ? p->get_opts(obj) : p->opts; - if(u32 & optmask) continue; + opts = p->get_opts ? p->get_opts(obj) : p->opts; + if(opts & optmask) continue; /* Sanity check */ assert(p->set || p->off); @@ -108,11 +109,11 @@ prop_write_values /* Write */ save = 0; cur = obj + p->off; - new = NULL; + snew = dnew = NULL; /* List */ if (p->islist) - new = (f->hmf_type == HMF_MAP) ? + snew = (f->hmf_type == HMF_MAP) ? htsmsg_field_get_map(f) : htsmsg_field_get_list(f); @@ -142,10 +143,10 @@ prop_write_values case PT_U32: { if (p->intextra && INTEXTRA_IS_SPLIT(p->intextra)) { char *s; - if (!(new = htsmsg_field_get_str(f))) + if (!(snew = htsmsg_field_get_str(f))) continue; - u32 = atol(new) * p->intextra; - if ((s = strchr(new, '.')) != NULL) + u32 = atol(snew) * p->intextra; + if ((s = strchr(snew, '.')) != NULL) u32 += (atol(s + 1) % p->intextra); } else { if (htsmsg_field_get_u32(f, &u32)) @@ -156,9 +157,9 @@ prop_write_values } case PT_S64: { if (p->intextra && INTEXTRA_IS_SPLIT(p->intextra)) { - if (!(new = htsmsg_field_get_str(f))) + if (!(snew = htsmsg_field_get_str(f))) continue; - s64 = prop_intsplit_from_str(new, p->intextra); + s64 = prop_intsplit_from_str(snew, p->intextra); } else { if (htsmsg_field_get_s64(f, &s64)) continue; @@ -176,12 +177,28 @@ prop_write_values } case PT_STR: { char **str = cur; - if (!(new = htsmsg_field_get_str(f))) + if (!(snew = htsmsg_field_get_str(f))) continue; - if (!p->set && strcmp((*str) ?: "", new)) { + if (opts & PO_TRIM) { + if (*(char *)snew <= ' ' || ((char *)snew)[strlen(snew)-1] <= ' ') { + const char *x = snew; + char *y; + while (*x && *x <= ' ') x++; + snew = dnew = strdup(x); + if (*x) { + y = dnew + strlen(dnew); + while (y != x) { + y--; + if (*y <= ' ') break; + } + *y = '\0'; + } + } + } + if (!p->set && strcmp((*str) ?: "", snew)) { /* make sure that the string is valid all time */ void *old = *str; - *str = strdup(new); + *str = strdup(snew); free(old); save = 1; } @@ -197,11 +214,11 @@ prop_write_values case PT_LANGSTR: { lang_str_t **lstr1 = cur; lang_str_t *lstr2; - new = htsmsg_field_get_map(f); - if (!new) + snew = htsmsg_field_get_map(f); + if (!snew) continue; if (!p->set) { - lstr2 = lang_str_deserialize_map((htsmsg_t *)new); + lstr2 = lang_str_deserialize_map((htsmsg_t *)snew); if (lang_str_compare(*lstr1, lstr2)) { lang_str_destroy(*lstr1); *lstr1 = lstr2; @@ -213,9 +230,9 @@ prop_write_values break; } case PT_PERM: { - if (!(new = htsmsg_field_get_str(f))) + if (!(snew = htsmsg_field_get_str(f))) continue; - u32 = (int)strtol(new,NULL,0); + u32 = (int)strtol(snew, NULL, 0); PROP_UPDATE(u32, uint32_t); break; } @@ -225,8 +242,12 @@ prop_write_values } /* Setter */ - if (p->set && new) - save = p->set(obj, new); + if (p->set && snew) + save = p->set(obj, snew); + + /* Remove dynamic contents */ + if (dnew) + free(dnew); /* Updated */ if (save) { diff --git a/src/prop.h b/src/prop.h index 392f3e6a9..c1f083723 100644 --- a/src/prop.h +++ b/src/prop.h @@ -65,6 +65,7 @@ typedef enum { #define PO_PERSIST (1<<17) // Persistent value (return back on save) #define PO_DOC (1<<18) // Use doc callback instead description if exists #define PO_DOC_NLIST (1<<19) // Do not show list in doc +#define PO_TRIM (1<<20) // Trim whitespaces (left & right) on load /* * min/max/step helpers