summaryrefslogtreecommitdiff
path: root/formats/format_pcm.c
diff options
context:
space:
mode:
authorMatthew Jordan <mjordan@digium.com>2012-04-16 20:17:03 +0000
committerMatthew Jordan <mjordan@digium.com>2012-04-16 20:17:03 +0000
commitba7032be5f4ac8c638806fe6d4f683ec7813c82f (patch)
tree4b20c302a1f4d8aed7f6247dacab56281ab2c34a /formats/format_pcm.c
parentba0f044bde5e5417cec7c3daf2bed907224423e6 (diff)
Check for IO stream failures in various format's truncate/seek operations
For the formats that support seek and/or truncate operations, many of the C library calls used to determine or set the current position indicator in the file stream were not being checked. In some situations, if an error occurred, a negative value would be returned from the library call. This could then be interpreted inappropriately as positional data. This patch checks the return values from these library calls before using them in subsequent operations. (issue ASTERISK-19655) Reported by: Matt Jordan Review: https://reviewboard.asterisk.org/r/1863/ ........ Merged revisions 362151 from http://svn.asterisk.org/svn/asterisk/branches/1.8 ........ Merged revisions 362152 from http://svn.asterisk.org/svn/asterisk/branches/10 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@362153 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'formats/format_pcm.c')
-rw-r--r--formats/format_pcm.c65
1 files changed, 55 insertions, 10 deletions
diff --git a/formats/format_pcm.c b/formats/format_pcm.c
index 3bf27388c..2a740ef5d 100644
--- a/formats/format_pcm.c
+++ b/formats/format_pcm.c
@@ -105,9 +105,20 @@ static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
off_t cur, max, offset = 0;
int ret = -1; /* assume error */
- cur = ftello(fs->f);
- fseeko(fs->f, 0, SEEK_END);
- max = ftello(fs->f);
+ if ((cur = ftello(fs->f)) < 0) {
+ ast_log(AST_LOG_WARNING, "Unable to determine current position in pcm filestream %p: %s\n", fs, strerror(errno));
+ return -1;
+ }
+
+ if (fseeko(fs->f, 0, SEEK_END) < 0) {
+ ast_log(AST_LOG_WARNING, "Unable to seek to end of pcm filestream %p: %s\n", fs, strerror(errno));
+ return -1;
+ }
+
+ if ((max = ftello(fs->f) < 0)) {
+ ast_log(AST_LOG_WARNING, "Unable to determine max position in pcm filestream %p: %s\n", fs, strerror(errno));
+ return -1;
+ }
switch (whence) {
case SEEK_SET:
@@ -151,7 +162,18 @@ static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
static int pcm_trunc(struct ast_filestream *fs)
{
- return ftruncate(fileno(fs->f), ftello(fs->f));
+ int cur, fd;
+
+ if ((fd = fileno(fs->f)) < 0) {
+ ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for pcm filestream %p: %s\n", fs, strerror(errno));
+ return -1;
+ }
+ if ((cur = ftello(fs->f) < 0)) {
+ ast_log(AST_LOG_WARNING, "Unable to determine current position in pcm filestream %p: %s\n", fs, strerror(errno));
+ return -1;
+ }
+ /* Truncate file to current length */
+ return ftruncate(fd, cur);
}
static off_t pcm_tell(struct ast_filestream *fs)
@@ -374,7 +396,7 @@ static int au_rewrite(struct ast_filestream *s, const char *comment)
/* XXX check this, probably incorrect */
static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
{
- off_t min, max, cur;
+ off_t min = AU_HEADER_SIZE, max, cur;
long offset = 0, bytes;
if (fs->fmt->format.id == AST_FORMAT_G722)
@@ -382,10 +404,20 @@ static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
else
bytes = sample_offset;
- min = AU_HEADER_SIZE;
- cur = ftello(fs->f);
- fseek(fs->f, 0, SEEK_END);
- max = ftello(fs->f);
+ if ((cur = ftello(fs->f)) < 0) {
+ ast_log(AST_LOG_WARNING, "Unable to determine current position in au filestream %p: %s\n", fs, strerror(errno));
+ return -1;
+ }
+
+ if (fseeko(fs->f, 0, SEEK_END) < 0) {
+ ast_log(AST_LOG_WARNING, "Unable to seek to end of au filestream %p: %s\n", fs, strerror(errno));
+ return -1;
+ }
+
+ if ((max = ftello(fs->f) < 0)) {
+ ast_log(AST_LOG_WARNING, "Unable to determine max position in au filestream %p: %s\n", fs, strerror(errno));
+ return -1;
+ }
if (whence == SEEK_SET)
offset = bytes + min;
@@ -406,8 +438,21 @@ static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
static int au_trunc(struct ast_filestream *fs)
{
- if (ftruncate(fileno(fs->f), ftell(fs->f)))
+ int fd;
+ off_t cur;
+
+ if ((fd = fileno(fs->f)) < 0) {
+ ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for au filestream %p: %s\n", fs, strerror(errno));
+ return -1;
+ }
+ if ((cur = ftello(fs->f) < 0)) {
+ ast_log(AST_LOG_WARNING, "Unable to determine current position in au filestream %p: %s\n", fs, strerror(errno));
return -1;
+ }
+ /* Truncate file to current length */
+ if (ftruncate(fd, cur)) {
+ return -1;
+ }
return update_header(fs->f);
}