[sr-dev] git:master:260a825a: core: parser/parse_hname2 - option to skip printing logs on parse failure

Daniel-Constantin Mierla miconda at gmail.com
Wed Jun 2 18:07:57 CEST 2021


Module: kamailio
Branch: master
Commit: 260a825a48ac6f8b845a4b934f86ea3e04c120d4
URL: https://github.com/kamailio/kamailio/commit/260a825a48ac6f8b845a4b934f86ea3e04c120d4

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2021-06-02T18:00:35+02:00

core: parser/parse_hname2 - option to skip printing logs on parse failure

---

Modified: src/core/parser/parse_hname2.c
Modified: src/core/parser/parse_hname2.h

---

Diff:  https://github.com/kamailio/kamailio/commit/260a825a48ac6f8b845a4b934f86ea3e04c120d4.diff
Patch: https://github.com/kamailio/kamailio/commit/260a825a48ac6f8b845a4b934f86ea3e04c120d4.patch

---

diff --git a/src/core/parser/parse_hname2.c b/src/core/parser/parse_hname2.c
index ab56fa4ea5..20398a9595 100644
--- a/src/core/parser/parse_hname2.c
+++ b/src/core/parser/parse_hname2.c
@@ -219,21 +219,25 @@ int ksr_hname_init_config(void)
  * - fills hdr structure (must not be null)
  * - set hdr->type=HDR_ERROR_T in case of parsing error
  * - if emode==1, then parsing does not expect : after header name
+ * - if logmode==1, then print error log messages on parsing failure
  * - returns pointer after : if emode==0 or after header name if emode==1
+ *   in case of parsing error, returns begin and sets hdr->type to HDR_ERROR_T
  */
 char *parse_sip_header_name(char* const begin, const char* const end,
-		hdr_field_t* const hdr, int emode)
+		hdr_field_t* const hdr, int emode, int logmode)
 {
 	char *p;
 	int i;
 
-	if (end <= begin) {
+	if (begin == NULL || end == NULL || end <= begin) {
 		hdr->type = HDR_ERROR_T;
 		return begin;
 	}
 	if(_ksr_hname_chars_idx[(unsigned char)(*begin)] == 0) {
-		LM_ERR("invalid start of header name for [%.*s]\n",
-				(int)(end-begin), begin);
+		if(likely(logmode)) {
+			LM_ERR("invalid start of header name for [%.*s]\n",
+					(int)(end-begin), begin);
+		}
 		hdr->type = HDR_ERROR_T;
 		return begin;
 	}
@@ -262,8 +266,10 @@ char *parse_sip_header_name(char* const begin, const char* const end,
 		}
 		if(*p != ' ' && *p != '\t') {
 			/* no white space - bad header name format */
-			LM_ERR("invalid header name for [%.*s]\n",
-					(int)(end-begin), begin);
+			if(likely(logmode)) {
+				LM_ERR("invalid header name for [%.*s]\n",
+						(int)(end-begin), begin);
+			}
 			hdr->type = HDR_ERROR_T;
 			return begin;
 		}
@@ -271,8 +277,10 @@ char *parse_sip_header_name(char* const begin, const char* const end,
 
 	if(p == end) {
 		/* no : found - emode==0 */
-		LM_ERR("invalid end of header name for [%.*s]\n",
-				(int)(end-begin), begin);
+		if(likely(logmode)) {
+			LM_ERR("invalid end of header name for [%.*s]\n",
+					(int)(end-begin), begin);
+		}
 		hdr->type = HDR_ERROR_T;
 		return begin;
 	}
@@ -298,7 +306,7 @@ char *parse_sip_header_name(char* const begin, const char* const end,
 
 char* parse_hname2(char* const begin, const char* const end, struct hdr_field* const hdr)
 {
-	return parse_sip_header_name(begin, end, hdr, 0);
+	return parse_sip_header_name(begin, end, hdr, 0, 1);
 }
 
 /**
@@ -307,11 +315,11 @@ char* parse_hname2(char* const begin, const char* const end, struct hdr_field* c
  */
 char* parse_hname2_short(char* const begin, const char* const end, struct hdr_field* const hdr)
 {
-	return parse_sip_header_name(begin, end, hdr, 0);
+	return parse_sip_header_name(begin, end, hdr, 0, 1);
 }
 
 char* parse_hname2_str (str* const hbuf, hdr_field_t* const hdr)
 {
-	return parse_sip_header_name(hbuf->s, hbuf->s + hbuf->len, hdr, 1);
+	return parse_sip_header_name(hbuf->s, hbuf->s + hbuf->len, hdr, 1, 1);
 }
 
diff --git a/src/core/parser/parse_hname2.h b/src/core/parser/parse_hname2.h
index 2c864857d6..fc52c1c802 100644
--- a/src/core/parser/parse_hname2.h
+++ b/src/core/parser/parse_hname2.h
@@ -35,6 +35,15 @@
 /** Fast 32-bit header field name parser.
  * @file
  */
+
+char *parse_sip_header_name(char* const begin, const char* const end,
+		hdr_field_t* const hdr, int emode, int logmode);
+
+/* macro to find header name without printing errors in failure case */
+#define find_hname2_str(hbuf, hdr) \
+	parse_sip_header_name((hbuf)->s, (hbuf)->s + (hbuf)->len, hdr, 1, 0)
+
+
 char* parse_hname2(char* const begin, const char* const end, struct hdr_field* const hdr);
 char* parse_hname2_short(char* const begin, const char* const end, struct hdr_field* const hdr);
 char* parse_hname2_str (str* const hbuf, hdr_field_t* const hdr);




More information about the sr-dev mailing list