[tin-dev] [PATCH] improved error message for unprintable chars in body

Urs Janßen urs at tin.org
Wed Jul 12 07:59:48 CEST 2023


- improved error message for unprintable chars in body
- one strdup() and one memmove() call replaced with wrappers
- some very minor performance tweaks

=== modified file 'src/filter.c'
--- src/filter.c	2023-05-09 22:05:25 +0000
+++ src/filter.c	2023-07-12 04:23:21 +0000
@@ -2116,7 +2116,7 @@
 									*e++ = ',';
 									skip = TRUE;
 								}
-								if (*s != ':' && !isspace((int) *s) && !skip)
+								if (!skip && *s != ':' && !isspace((int) *s))
 									*e++ = *s;
 								if (isspace((int) *s))
 									skip = FALSE;

=== modified file 'src/lang.c'
--- src/lang.c	2023-07-07 09:25:56 +0000
+++ src/lang.c	2023-07-07 14:21:58 +0000
@@ -1005,7 +1005,7 @@
          not  transport any  useful information,  they should be as  short as\n\
          possible.\n");
 constext txt_warn_suspicious_mail[] = N_("Warning: this mail address may contain a spamtrap. %s=continue, %s=abort? ");
-constext txt_warn_unprintable_char[] = N_("Warning: line %d contains unprintable chars:\n\t%s\n");
+constext txt_warn_unprintable_char[] = N_("Warning: line %d contains unprintable chars:\n%s\n");
 constext txt_warn_wrong_sig_format[] = N_("\nWarning: Signatures should start with '-- \\n' not with '--\\n'.\n");
 constext txt_writing_attributes_file[] = N_("Writing attributes file...");
 

=== modified file 'src/nntplib.c'
--- src/nntplib.c	2023-07-07 09:25:56 +0000
+++ src/nntplib.c	2023-07-11 22:24:51 +0000
@@ -838,7 +838,7 @@
 			if (verbose)	/* only log password when running verbose */
 				debug_print_file("NNTP", ">>>%s%s", logtime(), string);
 			else {
-				char *c = strdup(string);
+				char *c = my_strdup(string);
 				int l = 0;
 
 				if (!strncmp(string,"AUTHINFO PASS", 13))
@@ -2375,7 +2375,7 @@
 		return EOF;
 
 	/* move leftover input data to beginning of input buffer */
-	memmove(buf->z_rd_buf, buf->z_rd->next_in, buf->z_rd->avail_in);
+	my_memmove(buf->z_rd_buf, buf->z_rd->next_in, buf->z_rd->avail_in);
 	buf->z_rd->next_in = buf->z_rd_buf;
 
 	return (SZ(buf->rd.buf) - buf->rd.ub) - buf->z_rd->avail_out;

=== modified file 'src/post.c'
--- src/post.c	2023-07-07 09:25:56 +0000
+++ src/post.c	2023-07-07 14:37:40 +0000
@@ -1735,11 +1735,12 @@
 			int seen = 0; /* already reported a unprintable char in that line? */
 
 			/*
-			 * TODO:
+			 * TODO for txt_warn_unprintable_char:
+			 * - prefix message with "..." if cp != line?
+			 * - honor utf8_graphics?
 			 * - convert unprintable chars to octal values like
 			 *   in draw_pager_line()?
 			 * - do we need a Big5 exception (like in draw_pager_line())?
-			 * - is warning strong enough or shall we use error?
 			 */
 			col = 0;
 			for (cp = line; *cp; ) {
@@ -1749,34 +1750,34 @@
 				} else {
 #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE)
 					if ((num_bytes = mbtowc(&wc, cp, MB_CUR_MAX)) != -1) {
-						cp += num_bytes;
-						if (!contains_8bit && num_bytes > 1)
+						if (!contains_8bit && (num_bytes > 1 || !isascii(*cp)))
 							contains_8bit = TRUE;
 						if (iswprint((wint_t) wc) && ((wc_width = wcwidth(wc)) != -1))
 							col += wc_width;
 						else {
-							col++;
 							if (seen != cnt) { /* warn just once per line */
 								seen = cnt;
-								my_fprintf(stderr, _(txt_warn_unprintable_char), cnt, line);
+								my_fprintf(stderr, _(txt_warn_unprintable_char), cnt, strunc(cp, cCOLS - 1));
 								warnings++;
 							}
+							col++;
 						}
+						cp += num_bytes;
 					} else {
-						cp++;
-						col++;
 						if (seen != cnt) { /* warn just once per line */
 							seen = cnt;
-							my_fprintf(stderr, _(txt_warn_unprintable_char), cnt, line);
+							my_fprintf(stderr, _(txt_warn_unprintable_char), cnt, strunc(cp, cCOLS - 1));
 							warnings++;
 						}
+						cp++;
+						col++;
 					}
 #else
 					if (!contains_8bit && !isascii(*cp))
 						contains_8bit = TRUE;
 					if (!my_isprint(*cp) && seen != cnt) { /* warn just once per line */
 						seen = cnt;
-						my_fprintf(stderr, _(txt_warn_unprintable_char), cnt, line);
+						my_fprintf(stderr, _(txt_warn_unprintable_char), cnt, strunc(cp, cCOLS - 1));
 						warnings++;
 					}
 					cp++;

=== modified file 'src/refs.c'
--- src/refs.c	2023-05-10 04:42:49 +0000
+++ src/refs.c	2023-07-12 05:52:51 +0000
@@ -161,28 +161,34 @@
 /*
  * Checks if Message-ID has valid format
  * Returns TRUE if it does, FALSE if it does not
+ * modifies *msgid
  *
- * TODO: combine with post.c:damaged_id()
+ * TODO: combine with post.c:damaged_id(), which does not modify
+ *       its input
  */
 static t_bool
 valid_msgid(
 	char *msgid)
 {
-	size_t mlen = 0;
+	size_t mlen;
 	t_bool at_present = FALSE;
 
 	str_trim(msgid);
-	if (!msgid || *msgid != '<')
+	mlen = strlen(msgid);
+
+	if (!mlen || !msgid || *msgid != '<' || *(msgid + mlen -1) != '>')
 		return FALSE;
 
-	while (isascii((unsigned char) *msgid) && isgraph((unsigned char) *msgid) && !iscntrl((unsigned char) *msgid) && *msgid != '>') {
-		if (*msgid == '@')
+	while (*msgid) {
+		if (*msgid < 33 || *msgid > 126 || *msgid == '[' || *msgid == ']' || *msgid == '\\' || (*msgid == '>' && *(msgid + 1) != '\0'))
+			return FALSE;
+		if (!at_present && *msgid == '@')
 			at_present = TRUE;
-		mlen++;
+
 		msgid++;
 	}
 
-	if (!at_present || (*msgid != '>') || mlen <= 2 /* || mlen > 250 */|| *(msgid + 1))
+	if (!at_present || mlen < 5 /* || mlen > 250 */) /* we accept longer ids for now */
 		return FALSE;
 
 	return TRUE;

=== modified file 'src/string.c'
--- src/string.c	2023-02-22 07:57:18 +0000
+++ src/string.c	2023-07-11 23:10:32 +0000
@@ -527,7 +527,7 @@
 
 
 /*
- * str_trim - leading and trailing whitespace
+ * str_trim - in-place string trim leading and trailing whitespace
  *
  * INPUT:  string  - string to trim
  *
@@ -539,29 +539,32 @@
 str_trim(
 	char *string)
 {
-	char *rp;		/* reading string pointer */
-	char *wp;		/* writing string pointer */
-	char *ls;		/* last space */
+	char *rp, *wp, *ep;
+	size_t s;
 
 	if (string == NULL)
 		return NULL;
 
-	for (rp = wp = ls = string; isspace((int) *rp); rp++)		/* Skip leading space */
+	if (!(s = strlen(string)))
+		return string;
+
+	/* remove training spaces */
+	ep = string + s - 1;
+    while (ep >= string && isspace(*ep))
+        ep--;
+    *(ep + 1) = '\0';
+
+	/* skip leading space */
+	for (rp = wp = string; isspace((int) *rp); rp++)
 		;
 
-	while (*rp) {
-		if (isspace((int) *rp)) {
-			if (ls == NULL)		/* Remember last written space */
-				ls = wp;
-		} else
-			ls = NULL;			/* It wasn't the last space */
-		*wp++ = *rp++;
-	}
+	/* copy if required to keep address */
+	if (rp != string) {
+		while (*rp)
+			*wp++ = *rp++;
 
-	if (ls)						/* ie, there is trailing space */
-		*ls = '\0';
-	else
 		*wp = '\0';
+	}
 
 	return string;
 }




More information about the tin-dev mailing list