Browse code

core: tcp read - proper safety when priting log messages for read error case

- related to GH #748

(cherry picked from commit 48195546d7b5762be897c748bd3631f6b6a5c1d0)

Daniel-Constantin Mierla authored on 31/08/2016 13:05:51
Showing 1 changed files
... ...
@@ -1338,7 +1338,7 @@ again:
1338 1338
 			 * if req. is complete we might have a second unparsed
1339 1339
 			 * request after it, so postpone release_with_eof
1340 1340
 			 */
1341
-			if (unlikely((con->state==S_CONN_EOF) && 
1341
+			if (unlikely((con->state==S_CONN_EOF) &&
1342 1342
 						(! TCP_REQ_COMPLETE(req)))) {
1343 1343
 				LM_DBG("EOF\n");
1344 1344
 				resp=CONN_EOF;
... ...
@@ -1346,10 +1346,19 @@ again:
1346 1346
 			}
1347 1347
 		}
1348 1348
 		if (unlikely(req->error!=TCP_REQ_OK)){
1349
-			LM_ERR("bad request, state=%d, error=%d buf:\n%.*s\nparsed:\n%.*s\n",
1349
+			if(req->buf!=NULL && req->start!=NULL && req->pos!=NULL
1350
+					&& req->pos>=req->buf && req->parsed>=req->start) {
1351
+				LM_ERR("bad request, state=%d, error=%d buf:\n%.*s\nparsed:\n%.*s\n",
1350 1352
 					req->state, req->error,
1351 1353
 					(int)(req->pos-req->buf), req->buf,
1352 1354
 					(int)(req->parsed-req->start), req->start);
1355
+			} else {
1356
+				LM_ERR("bad request, state=%d, error=%d buf:%d - %p,"
1357
+						" parsed:%d - %p\n",
1358
+					req->state, req->error,
1359
+					(int)(req->pos-req->buf), req->buf,
1360
+					(int)(req->parsed-req->start), req->start);
1361
+			}
1353 1362
 			LM_DBG("received from: port %d\n", con->rcv.src_port);
1354 1363
 			print_ip("received from: ip", &con->rcv.src_ip, "\n");
1355 1364
 			resp=CONN_ERROR;