[asterisk-commits] cleanup: Fix fread() and fwrite() error handling (asterisk[13])

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Apr 27 12:08:15 CDT 2017


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/5510 )

Change subject: cleanup: Fix fread() and fwrite() error handling
......................................................................


cleanup: Fix fread() and fwrite() error handling

Cleaned up some of the incorrect uses of fread() and fwrite(), mostly in
the format modules. Neither of these functions will ever return a value
less than 0, which we were checking for in some cases.

I've introduced a fair amount of duplication in the format modules, but
I plan to change how format modules work internally in a subsequent
patch set, so this is simply a stop-gap.

Change-Id: I8ca1cd47c20b2c0b72088bd13b9046f6977aa872
---
M addons/format_mp3.c
M apps/app_minivm.c
M apps/app_voicemail.c
M formats/format_g719.c
M formats/format_g723.c
M formats/format_g726.c
M formats/format_g729.c
M formats/format_gsm.c
M formats/format_h263.c
M formats/format_h264.c
M formats/format_ilbc.c
M formats/format_ogg_vorbis.c
M formats/format_pcm.c
M formats/format_siren14.c
M formats/format_siren7.c
M formats/format_sln.c
M formats/format_vox.c
M formats/format_wav.c
M formats/format_wav_gsm.c
M main/http.c
M main/manager.c
21 files changed, 227 insertions(+), 70 deletions(-)

Approvals:
  Kevin Harwell: Looks good to me, approved
  Richard Mudgett: Looks good to me, but someone else must approve
  Jenkins2: Approved for Submit
  Joshua Colp: Looks good to me, but someone else must approve



diff --git a/addons/format_mp3.c b/addons/format_mp3.c
index e354148..1373f3e 100644
--- a/addons/format_mp3.c
+++ b/addons/format_mp3.c
@@ -120,9 +120,11 @@
 
 	res = ftell(s->f);
 	p->sbuflen = fread(p->sbuf, 1, MP3_SCACHE, s->f);
-	if(p->sbuflen < 0) {
-		ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", p->sbuflen, strerror(errno));
-		return -1;
+	if (p->sbuflen < MP3_SCACHE) {
+		if (ferror(s->f)) {
+			ast_log(LOG_WARNING, "Error while reading MP3 file: %s\n", strerror(errno));
+			return -1;
+		}
 	}
 	res = decodeMP3(&p->mp,p->sbuf,p->sbuflen,p->dbuf,MP3_DCACHE,&p->dbuflen);
 	if(res != MP3_OK)
diff --git a/apps/app_minivm.c b/apps/app_minivm.c
index da2956f..1bfcfbb 100644
--- a/apps/app_minivm.c
+++ b/apps/app_minivm.c
@@ -856,16 +856,16 @@
 	if (bio->ateof)
 		return 0;
 
-	if ((l = fread(bio->iobuf, 1, B64_BASEMAXINLINE,fi)) <= 0) {
-		if (ferror(fi))
-			return -1;
-
+	if ((l = fread(bio->iobuf, 1, B64_BASEMAXINLINE, fi)) != B64_BASEMAXINLINE) {
 		bio->ateof = 1;
-		return 0;
+		if (l == 0) {
+			/* Assume EOF */
+			return 0;
+		}
 	}
 
-	bio->iolen= l;
-	bio->iocp= 0;
+	bio->iolen = l;
+	bio->iocp = 0;
 
 	return 1;
 }
diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index b237534..f62c7d8 100644
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -2730,9 +2730,9 @@
 			*(vmu->email) = '\0';
 		return -1;
 	}
-	if (fread(buf, len, 1, p) < len) {
+	if (fread(buf, 1, len, p) != len) {
 		if (ferror(p)) {
-			ast_log(LOG_ERROR, "Short read while reading in mail file.\n");
+			ast_log(LOG_ERROR, "Error while reading mail file: %s\n");
 			return -1;
 		}
 	}
@@ -4745,12 +4745,12 @@
 	if (bio->ateof)
 		return 0;
 
-	if ((l = fread(bio->iobuf, 1, BASEMAXINLINE, fi)) <= 0) {
-		if (ferror(fi))
-			return -1;
-
+	if ((l = fread(bio->iobuf, 1, BASEMAXINLINE, fi)) != BASEMAXINLINE) {
 		bio->ateof = 1;
-		return 0;
+		if (l == 0) {
+			/* Assume EOF */
+			return 0;
+		}
 	}
 
 	bio->iolen = l;
diff --git a/formats/format_g719.c b/formats/format_g719.c
index f582862..b93cf0b 100644
--- a/formats/format_g719.c
+++ b/formats/format_g719.c
@@ -47,8 +47,16 @@
 
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
 	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	*whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
diff --git a/formats/format_g723.c b/formats/format_g723.c
index 926c65b..5eed3f5 100644
--- a/formats/format_g723.c
+++ b/formats/format_g723.c
@@ -66,8 +66,17 @@
 	}
 	/* Read the data into the buffer */
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, size);
-	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != size) {
-		ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
+	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	*whennext = s->fr.samples = 240;
diff --git a/formats/format_g726.c b/formats/format_g726.c
index cadb2d9..0455d6d 100644
--- a/formats/format_g726.c
+++ b/formats/format_g726.c
@@ -126,8 +126,16 @@
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]);
 	s->fr.samples = 8 * FRAME_TIME;
 	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	*whennext = s->fr.samples;
diff --git a/formats/format_g729.c b/formats/format_g729.c
index 4133546..fa122d3 100644
--- a/formats/format_g729.c
+++ b/formats/format_g729.c
@@ -53,8 +53,16 @@
 	s->fr.samples = G729A_SAMPLES;
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
 	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-		if (res && (res != 10))	/* XXX what for ? */
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	*whennext = s->fr.samples;
diff --git a/formats/format_gsm.c b/formats/format_gsm.c
index 54c4b8f..07aba9f 100644
--- a/formats/format_gsm.c
+++ b/formats/format_gsm.c
@@ -57,10 +57,18 @@
 {
 	int res;
 
-	AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
+	AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE);
 	if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), GSM_FRAME_SIZE, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	*whennext = s->fr.samples = GSM_SAMPLES;
@@ -133,7 +141,7 @@
 		int i;
 		fseeko(fs->f, 0, SEEK_END);
 		for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
-			if (!fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f)) {
+			if (fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f) != GSM_FRAME_SIZE) {
 				ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
 			}
 		}
diff --git a/formats/format_h263.c b/formats/format_h263.c
index 67f1269..5c23923 100644
--- a/formats/format_h263.c
+++ b/formats/format_h263.c
@@ -60,7 +60,7 @@
 {
 	unsigned int ts;
 
-	if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) {
+	if (fread(&ts, 1, sizeof(ts), s->f) != sizeof(ts)) {
 		ast_log(LOG_WARNING, "Empty file!\n");
 		return -1;
 	}
@@ -76,7 +76,7 @@
 	struct h263_desc *fs = (struct h263_desc *)s->_private;
 
 	/* Send a frame from the file to the appropriate channel */
-	if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
+	if ((res = fread(&len, 1, sizeof(len), s->f)) != sizeof(len))
 		return NULL;
 	len = ntohs(len);
 	mark = (len & FRAME_ENDED) ? 1 : 0;
@@ -87,8 +87,16 @@
 	}
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
 	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	s->fr.samples = fs->lastts;	/* XXX what ? */
diff --git a/formats/format_h264.c b/formats/format_h264.c
index 2ce7a59..5051fd4 100644
--- a/formats/format_h264.c
+++ b/formats/format_h264.c
@@ -52,7 +52,7 @@
 static int h264_open(struct ast_filestream *s)
 {
 	unsigned int ts;
-	if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) {
+	if (fread(&ts, 1, sizeof(ts), s->f) != sizeof(ts)) {
 		ast_log(LOG_WARNING, "Empty file!\n");
 		return -1;
 	}
@@ -68,7 +68,7 @@
 	struct h264_desc *fs = (struct h264_desc *)s->_private;
 
 	/* Send a frame from the file to the appropriate channel */
-	if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
+	if ((res = fread(&len, 1, sizeof(len), s->f)) != sizeof(len))
 		return NULL;
 	len = ntohs(len);
 	mark = (len & FRAME_ENDED) ? 1 : 0;
@@ -79,8 +79,16 @@
 	}
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
 	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d of %d) (%s)!\n", res, len, strerror(errno));
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	s->fr.samples = fs->lastts;
diff --git a/formats/format_ilbc.c b/formats/format_ilbc.c
index 0e2a92d..2d67ab2 100644
--- a/formats/format_ilbc.c
+++ b/formats/format_ilbc.c
@@ -51,8 +51,16 @@
 	/* Send a frame from the file to the appropriate channel */
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, ILBC_BUF_SIZE);
 	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	*whennext = s->fr.samples = ILBC_SAMPLES;
diff --git a/formats/format_ogg_vorbis.c b/formats/format_ogg_vorbis.c
index cf3b1c0..a7a9447 100644
--- a/formats/format_ogg_vorbis.c
+++ b/formats/format_ogg_vorbis.c
@@ -183,10 +183,10 @@
 	while (!tmp->eos) {
 		if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
 			break;
-		if (!fwrite(tmp->og.header, 1, tmp->og.header_len, s->f)) {
+		if (fwrite(tmp->og.header, 1, tmp->og.header_len, s->f) != tmp->og.header_len) {
 			ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
 		}
-		if (!fwrite(tmp->og.body, 1, tmp->og.body_len, s->f)) {
+		if (fwrite(tmp->og.body, 1, tmp->og.body_len, s->f) != tmp->og.body_len) {
 			ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
 		}
 		if (ogg_page_eos(&tmp->og))
@@ -213,10 +213,10 @@
 				if (ogg_stream_pageout(&s->os, &s->og) == 0) {
 					break;
 				}
-				if (!fwrite(s->og.header, 1, s->og.header_len, f)) {
-				ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
+				if (fwrite(s->og.header, 1, s->og.header_len, f) != s->og.header_len) {
+					ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
 				}
-				if (!fwrite(s->og.body, 1, s->og.body_len, f)) {
+				if (fwrite(s->og.body, 1, s->og.body_len, f) != s->og.body_len) {
 					ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
 				}
 				if (ogg_page_eos(&s->og)) {
diff --git a/formats/format_pcm.c b/formats/format_pcm.c
index 05a7eab..cc32ca1 100644
--- a/formats/format_pcm.c
+++ b/formats/format_pcm.c
@@ -85,9 +85,17 @@
 	/* Send a frame from the file to the appropriate channel */
 
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
-	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	s->fr.datalen = res;
@@ -142,9 +150,10 @@
 		const char *src = (ast_format_cmp(fs->fmt->format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) ? alaw_silence : ulaw_silence;
 
 		while (left) {
-			size_t written = fwrite(src, 1, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
-			if (written == -1)
+			size_t written = fwrite(src, 1, MIN(left, BUF_SIZE), fs->f);
+			if (written < MIN(left, BUF_SIZE)) {
 				break;	/* error */
+			}
 			left -= written;
 		}
 		ret = 0; /* successful */
@@ -212,7 +221,10 @@
 				to_write = fpos - cur;
 				if (to_write > sizeof(buf))
 					to_write = sizeof(buf);
-				fwrite(buf, 1, to_write, fs->f);
+				if (fwrite(buf, 1, to_write, fs->f) != to_write) {
+					ast_log(LOG_ERROR, "Failed to write to file: %s\n", strerror(errno));
+					return -1;
+				}
 				cur += to_write;
 			}
 		}
diff --git a/formats/format_siren14.c b/formats/format_siren14.c
index d0a16c4..b35ebce 100644
--- a/formats/format_siren14.c
+++ b/formats/format_siren14.c
@@ -47,8 +47,16 @@
 
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
 	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	*whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
diff --git a/formats/format_siren7.c b/formats/format_siren7.c
index acfde42..c7856b2 100644
--- a/formats/format_siren7.c
+++ b/formats/format_siren7.c
@@ -47,8 +47,16 @@
 
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
 	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	*whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
diff --git a/formats/format_sln.c b/formats/format_sln.c
index 67e5cd5..aa47bde 100644
--- a/formats/format_sln.c
+++ b/formats/format_sln.c
@@ -40,9 +40,17 @@
 	/* Send a frame from the file to the appropriate channel */
 
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, buf_size);
-	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	*whennext = s->fr.samples = res/2;
diff --git a/formats/format_vox.c b/formats/format_vox.c
index 6cef72d..e4d1e37 100644
--- a/formats/format_vox.c
+++ b/formats/format_vox.c
@@ -46,9 +46,17 @@
 
 	/* Send a frame from the file to the appropriate channel */
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
-	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	*whennext = s->fr.samples = res * 2;
diff --git a/formats/format_wav.c b/formats/format_wav.c
index 90095eb..81807c1 100644
--- a/formats/format_wav.c
+++ b/formats/format_wav.c
@@ -363,7 +363,7 @@
 
 	/* Pad to even length */
 	if (fs->bytes & 0x1) {
-		if (!fwrite(&zero, 1, 1, s->f)) {
+		if (fwrite(&zero, 1, 1, s->f) != 1) {
 			ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
 		}
 	}
@@ -387,14 +387,23 @@
 	here = ftello(s->f);
 	if (fs->maxlen - here < bytes)		/* truncate if necessary */
 		bytes = fs->maxlen - here;
-	if (bytes < 0)
-		bytes = 0;
+	if (bytes <= 0) {
+		return NULL;
+	}
 /* 	ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
-	
-	if ( (res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) <= 0 ) {
-		if (res)
-			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+
+	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
+		if (feof(s->f)) {
+			if (res) {
+				ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+						"(expected %d bytes, read %d)\n",
+						ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+			}
+		} else {
+			ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+					ast_format_get_name(s->fr.subclass.format), strerror(errno));
+		}
 		return NULL;
 	}
 	s->fr.datalen = res;
diff --git a/formats/format_wav_gsm.c b/formats/format_wav_gsm.c
index 5b3853e..f11b272 100644
--- a/formats/format_wav_gsm.c
+++ b/formats/format_wav_gsm.c
@@ -424,8 +424,16 @@
 		int res;
 		
 		if ((res = fread(msdata, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) {
-			if (res && (res != 1))
-				ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+			if (feof(s->f)) {
+				if (res) {
+					ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+							"(expected %d bytes, read %d)\n",
+							ast_format_get_name(s->fr.subclass.format), MSGSM_FRAME_SIZE, res);
+				}
+			} else {
+				ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+						ast_format_get_name(s->fr.subclass.format), strerror(errno));
+			}
 			return NULL;
 		}
 		/* Convert from MS format to two real GSM frames */
@@ -513,7 +521,7 @@
 		int i;
 		fseek(fs->f, 0, SEEK_END);
 		for (i=0; i< (offset - max) / MSGSM_FRAME_SIZE; i++) {
-			if (!fwrite(msgsm_silence, 1, MSGSM_FRAME_SIZE, fs->f)) {
+			if (fwrite(msgsm_silence, 1, MSGSM_FRAME_SIZE, fs->f) != MSGSM_FRAME_SIZE) {
 				ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
 			}
 		}
diff --git a/main/http.c b/main/http.c
index 80c7b3c..cccc60b 100644
--- a/main/http.c
+++ b/main/http.c
@@ -526,6 +526,10 @@
 	/* send content */
 	if (method != AST_HTTP_HEAD || status_code >= 400) {
 		if (out && ast_str_strlen(out)) {
+			/*
+			 * NOTE: Because ser->f is a non-standard FILE *, fwrite() will probably not
+			 * behave exactly as documented.
+			 */
 			if (fwrite(ast_str_buffer(out), ast_str_strlen(out), 1, ser->f) != 1) {
 				ast_log(LOG_ERROR, "fwrite() failed: %s\n", strerror(errno));
 				close_connection = 1;
@@ -537,6 +541,10 @@
 			int len;
 
 			while ((len = read(fd, buf, sizeof(buf))) > 0) {
+				/*
+				 * NOTE: Because ser->f is a non-standard FILE *, fwrite() will probably not
+				 * behave exactly as documented.
+				 */
 				if (fwrite(buf, len, 1, ser->f) != 1) {
 					ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
 					close_connection = 1;
@@ -923,6 +931,11 @@
 {
 	int res;
 
+	/*
+	 * NOTE: Because ser->f is a non-standard FILE *, fread() does not behave as
+	 * documented.
+	 */
+
 	/* Stay in fread until get all the expected data or timeout. */
 	res = fread(buf, length, 1, ser->f);
 	if (res < 1) {
@@ -949,6 +962,11 @@
 {
 	int res;
 	char buf[MAX_HTTP_LINE_LENGTH];/* Discard buffer */
+
+	/*
+	 * NOTE: Because ser->f is a non-standard FILE *, fread() does not behave as
+	 * documented.
+	 */
 
 	/* Stay in fread until get all the expected data or timeout. */
 	while (sizeof(buf) < length) {
@@ -1066,6 +1084,11 @@
 	int res;
 	char chunk_sync[2];
 
+	/*
+	 * NOTE: Because ser->f is a non-standard FILE *, fread() does not behave as
+	 * documented.
+	 */
+
 	/* Stay in fread until get the expected CRLF or timeout. */
 	res = fread(chunk_sync, sizeof(chunk_sync), 1, ser->f);
 	if (res < 1) {
diff --git a/main/manager.c b/main/manager.c
index 0f7adf0..0ea6bfa 100644
--- a/main/manager.c
+++ b/main/manager.c
@@ -6458,6 +6458,12 @@
 	}
 
 	ao2_lock(s->session);
+	/*
+	 * It is worth noting here that you can all but ignore fread()'s documentation
+	 * for the purposes of this call. The FILE * we are working with here was created
+	 * as a result of a call to fopencookie() (or equivalent) in tcptls.c, and as such
+	 * the behavior of fread() is not as documented. Frankly, I think this is gross.
+	 */
 	res = fread(src + s->session->inlen, 1, maxlen - s->session->inlen, s->session->f);
 	if (res < 1) {
 		res = -1;	/* error return */

-- 
To view, visit https://gerrit.asterisk.org/5510
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I8ca1cd47c20b2c0b72088bd13b9046f6977aa872
Gerrit-PatchSet: 4
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: Sean Bright <sean.bright at gmail.com>



More information about the asterisk-commits mailing list