From: Matthew Jordan Date: Mon, 16 Apr 2012 19:30:16 +0000 (+0000) Subject: Check for IO stream failures in various format's truncate/seek operations X-Git-Tag: 1.8.13.0-rc1~55 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b3a38a51c73d6928c0014ad6a6d8604c18e88a40;p=thirdparty%2Fasterisk.git 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/ git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.8@362151 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/formats/format_g719.c b/formats/format_g719.c index d319fa8b49..692edad35b 100644 --- a/formats/format_g719.c +++ b/formats/format_g719.c @@ -82,11 +82,20 @@ static int g719seek(struct ast_filestream *fs, off_t sample_offset, int whence) sample_offset = SAMPLES_TO_BYTES(sample_offset); - cur = ftello(fs->f); + if ((cur = ftello(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } - fseeko(fs->f, 0, SEEK_END); + if (fseeko(fs->f, 0, SEEK_END) < 0) { + ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } - max = ftello(fs->f); + if ((max = ftello(fs->f) < 0)) { + ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } if (whence == SEEK_SET) offset = sample_offset; @@ -106,7 +115,18 @@ static int g719seek(struct ast_filestream *fs, off_t sample_offset, int whence) static int g719trunc(struct ast_filestream *fs) { - return ftruncate(fileno(fs->f), ftello(fs->f)); + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g719 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 g719 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + return ftruncate(fd, cur); } static off_t g719tell(struct ast_filestream *fs) diff --git a/formats/format_g723.c b/formats/format_g723.c index 7fe1a0a0c7..4d1c2cd56f 100644 --- a/formats/format_g723.c +++ b/formats/format_g723.c @@ -118,10 +118,19 @@ static int g723_seek(struct ast_filestream *fs, off_t sample_offset, int whence) static int g723_trunc(struct ast_filestream *fs) { - /* Truncate file to current length */ - if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0) + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g723 filestream %p: %s\n", fs, strerror(errno)); return -1; - return 0; + } + if ((cur = ftello(fs->f) < 0)) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in g723 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Truncate file to current length */ + return ftruncate(fd, cur); } static off_t g723_tell(struct ast_filestream *fs) diff --git a/formats/format_g729.c b/formats/format_g729.c index fcb7c1e6ac..5863ccec83 100644 --- a/formats/format_g729.c +++ b/formats/format_g729.c @@ -113,10 +113,19 @@ static int g729_seek(struct ast_filestream *fs, off_t sample_offset, int whence) static int g729_trunc(struct ast_filestream *fs) { - /* Truncate file to current length */ - if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0) + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g729 filestream %p: %s\n", fs, strerror(errno)); return -1; - return 0; + } + if ((cur = ftello(fs->f) < 0)) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in g729 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Truncate file to current length */ + return ftruncate(fd, cur); } static off_t g729_tell(struct ast_filestream *fs) diff --git a/formats/format_gsm.c b/formats/format_gsm.c index 91225d3343..e7aa24e27f 100644 --- a/formats/format_gsm.c +++ b/formats/format_gsm.c @@ -108,20 +108,33 @@ static int gsm_write(struct ast_filestream *fs, struct ast_frame *f) static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence) { - off_t offset=0,min,cur,max,distance; - - min = 0; - cur = ftello(fs->f); - fseeko(fs->f, 0, SEEK_END); - max = ftello(fs->f); + off_t offset = 0, min = 0, cur, max, distance; + + if ((cur = ftello(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 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 g719 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 g719 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* have to fudge to frame here, so not fully to sample */ - distance = (sample_offset/GSM_SAMPLES) * GSM_FRAME_SIZE; - if(whence == SEEK_SET) + distance = (sample_offset / GSM_SAMPLES) * GSM_FRAME_SIZE; + if (whence == SEEK_SET) { offset = distance; - else if(whence == SEEK_CUR || whence == SEEK_FORCECUR) + } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) { offset = distance + cur; - else if(whence == SEEK_END) + } else if (whence == SEEK_END) { offset = max - distance; + } + /* Always protect against seeking past the begining. */ offset = (offset < min)?min:offset; if (whence != SEEK_FORCECUR) { @@ -140,13 +153,31 @@ static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence) static int gsm_trunc(struct ast_filestream *fs) { - return ftruncate(fileno(fs->f), ftello(fs->f)); + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for gsm 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 gsm filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Truncate file to current length */ + return ftruncate(fd, cur); } static off_t gsm_tell(struct ast_filestream *fs) { off_t offset = ftello(fs->f); - return (offset/GSM_FRAME_SIZE)*GSM_SAMPLES; + + if (offset < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine offset for gsm filestream %p: %s\n", fs, strerror(errno)); + return 0; + } + + return (offset / GSM_FRAME_SIZE) * GSM_SAMPLES; } static const struct ast_format gsm_f = { diff --git a/formats/format_h263.c b/formats/format_h263.c index 2de224a815..757437c8a6 100644 --- a/formats/format_h263.c +++ b/formats/format_h263.c @@ -149,10 +149,19 @@ static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence) static int h263_trunc(struct ast_filestream *fs) { - /* Truncate file to current length */ - if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0) + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h263 filestream %p: %s\n", fs, strerror(errno)); return -1; - return 0; + } + if ((cur = ftello(fs->f) < 0)) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in h263 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Truncate file to current length */ + return ftruncate(fd, cur); } static off_t h263_tell(struct ast_filestream *fs) diff --git a/formats/format_h264.c b/formats/format_h264.c index 84078d14fd..84e94aa7a5 100644 --- a/formats/format_h264.c +++ b/formats/format_h264.c @@ -138,10 +138,19 @@ static int h264_seek(struct ast_filestream *fs, off_t sample_offset, int whence) static int h264_trunc(struct ast_filestream *fs) { - /* Truncate file to current length */ - if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0) + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h264 filestream %p: %s\n", fs, strerror(errno)); return -1; - return 0; + } + if ((cur = ftello(fs->f) < 0)) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in h264 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Truncate file to current length */ + return ftruncate(fd, cur); } static off_t h264_tell(struct ast_filestream *fs) diff --git a/formats/format_ilbc.c b/formats/format_ilbc.c index f620fd48e5..b7673d8ef0 100644 --- a/formats/format_ilbc.c +++ b/formats/format_ilbc.c @@ -111,10 +111,19 @@ static int ilbc_seek(struct ast_filestream *fs, off_t sample_offset, int whence) static int ilbc_trunc(struct ast_filestream *fs) { - /* Truncate file to current length */ - if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0) + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for iLBC filestream %p: %s\n", fs, strerror(errno)); return -1; - return 0; + } + if ((cur = ftello(fs->f) < 0)) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in iLBC filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Truncate file to current length */ + return ftruncate(fd, cur); } static off_t ilbc_tell(struct ast_filestream *fs) diff --git a/formats/format_pcm.c b/formats/format_pcm.c index 6985d2715b..9dfdaa8124 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 == 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); } diff --git a/formats/format_siren14.c b/formats/format_siren14.c index 0510800a48..067de76923 100644 --- a/formats/format_siren14.c +++ b/formats/format_siren14.c @@ -82,11 +82,20 @@ static int siren14seek(struct ast_filestream *fs, off_t sample_offset, int whenc sample_offset = SAMPLES_TO_BYTES(sample_offset); - cur = ftello(fs->f); + if ((cur = ftello(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in siren14 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } - fseeko(fs->f, 0, SEEK_END); + if (fseeko(fs->f, 0, SEEK_END) < 0) { + ast_log(AST_LOG_WARNING, "Unable to seek to end of siren14 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } - max = ftello(fs->f); + if ((max = ftello(fs->f) < 0)) { + ast_log(AST_LOG_WARNING, "Unable to determine max position in siren14 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } if (whence == SEEK_SET) offset = sample_offset; @@ -106,7 +115,19 @@ static int siren14seek(struct ast_filestream *fs, off_t sample_offset, int whenc static int siren14trunc(struct ast_filestream *fs) { - return ftruncate(fileno(fs->f), ftello(fs->f)); + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for siren14 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 siren14 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Truncate file to current length */ + return ftruncate(fd, cur); } static off_t siren14tell(struct ast_filestream *fs) diff --git a/formats/format_siren7.c b/formats/format_siren7.c index 2a01e1b08d..3a6b3b39c6 100644 --- a/formats/format_siren7.c +++ b/formats/format_siren7.c @@ -82,11 +82,20 @@ static int siren7seek(struct ast_filestream *fs, off_t sample_offset, int whence sample_offset = SAMPLES_TO_BYTES(sample_offset); - cur = ftello(fs->f); + if ((cur = ftello(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in siren7 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } - fseeko(fs->f, 0, SEEK_END); + if (fseeko(fs->f, 0, SEEK_END) < 0) { + ast_log(AST_LOG_WARNING, "Unable to seek to end of siren7 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } - max = ftello(fs->f); + if ((max = ftello(fs->f) < 0)) { + ast_log(AST_LOG_WARNING, "Unable to determine max position in siren7 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } if (whence == SEEK_SET) offset = sample_offset; @@ -106,7 +115,19 @@ static int siren7seek(struct ast_filestream *fs, off_t sample_offset, int whence static int siren7trunc(struct ast_filestream *fs) { - return ftruncate(fileno(fs->f), ftello(fs->f)); + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for siren7 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 siren7 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Truncate file to current length */ + return ftruncate(fd, cur); } static off_t siren7tell(struct ast_filestream *fs) diff --git a/formats/format_sln.c b/formats/format_sln.c index 4227547e2d..d6c18d688b 100644 --- a/formats/format_sln.c +++ b/formats/format_sln.c @@ -76,13 +76,25 @@ static int slinear_write(struct ast_filestream *fs, struct ast_frame *f) static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence) { - off_t offset=0,min,cur,max; + off_t offset=0, min = 0, cur, max; - min = 0; sample_offset <<= 1; - 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 sln 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 sln 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 sln filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + if (whence == SEEK_SET) offset = sample_offset; else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) @@ -99,7 +111,19 @@ static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int when static int slinear_trunc(struct ast_filestream *fs) { - return ftruncate(fileno(fs->f), ftello(fs->f)); + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for sln 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 sln filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Truncate file to current length */ + return ftruncate(fd, cur); } static off_t slinear_tell(struct ast_filestream *fs) diff --git a/formats/format_sln16.c b/formats/format_sln16.c index d999f68761..4cc099057e 100644 --- a/formats/format_sln16.c +++ b/formats/format_sln16.c @@ -82,11 +82,20 @@ static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int when sample_offset <<= 1; - cur = ftello(fs->f); + if ((cur = ftello(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in sln16 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } - fseeko(fs->f, 0, SEEK_END); + if (fseeko(fs->f, 0, SEEK_END) < 0) { + ast_log(AST_LOG_WARNING, "Unable to seek to end of sln16 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } - max = ftello(fs->f); + if ((max = ftello(fs->f) < 0)) { + ast_log(AST_LOG_WARNING, "Unable to determine max position in sln16 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } if (whence == SEEK_SET) offset = sample_offset; @@ -106,7 +115,19 @@ static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int when static int slinear_trunc(struct ast_filestream *fs) { - return ftruncate(fileno(fs->f), ftello(fs->f)); + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for sln16 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 sln16 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Truncate file to current length */ + return ftruncate(fd, cur); } static off_t slinear_tell(struct ast_filestream *fs) diff --git a/formats/format_vox.c b/formats/format_vox.c index eba299c486..a60808a11d 100644 --- a/formats/format_vox.c +++ b/formats/format_vox.c @@ -78,32 +78,54 @@ static int vox_write(struct ast_filestream *s, struct ast_frame *f) static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence) { - off_t offset=0,min,cur,max,distance; - - min = 0; - cur = ftello(fs->f); - fseeko(fs->f, 0, SEEK_END); - max = ftello(fs->f); - - /* have to fudge to frame here, so not fully to sample */ - distance = sample_offset/2; - if(whence == SEEK_SET) - offset = distance; - else if(whence == SEEK_CUR || whence == SEEK_FORCECUR) - offset = distance + cur; - else if(whence == SEEK_END) - offset = max - distance; - if (whence != SEEK_FORCECUR) { - offset = (offset > max)?max:offset; - offset = (offset < min)?min:offset; - } - return fseeko(fs->f, offset, SEEK_SET); + off_t offset = 0, min = 0, cur, max, distance; + + if ((cur = ftello(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 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 g719 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 g719 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + + /* have to fudge to frame here, so not fully to sample */ + distance = sample_offset/2; + if (whence == SEEK_SET) { + offset = distance; + } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) { + offset = distance + cur; + } else if (whence == SEEK_END) { + offset = max - distance; + } + if (whence != SEEK_FORCECUR) { + offset = (offset > max)?max:offset; + offset = (offset < min)?min:offset; + } + return fseeko(fs->f, offset, SEEK_SET); } static int vox_trunc(struct ast_filestream *fs) { - return ftruncate(fileno(fs->f), ftello(fs->f)); -} + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for vox 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 vox filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Truncate file to current length */ + return ftruncate(fd, cur);} static off_t vox_tell(struct ast_filestream *fs) { diff --git a/formats/format_wav.c b/formats/format_wav.c index 2c1c3eca1a..c5e47ed19b 100644 --- a/formats/format_wav.c +++ b/formats/format_wav.c @@ -42,6 +42,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #define WAV_BUF_SIZE 320 +#define WAV_HEADER_SIZE 44 + struct wav_desc { /* format-specific parameters */ int hz; int bytes; @@ -452,13 +454,25 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f) static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence) { - off_t min, max, cur, offset = 0, samples; + off_t min = WAV_HEADER_SIZE, max, cur, offset = 0, samples; samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */ - min = 44; /* wav header is 44 bytes */ - 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 wav 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 wav 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 wav filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + if (whence == SEEK_SET) offset = samples + min; else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) @@ -475,8 +489,21 @@ static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence) static int wav_trunc(struct ast_filestream *fs) { - if (ftruncate(fileno(fs->f), ftello(fs->f))) + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for wav 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 wav 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); } diff --git a/formats/format_wav_gsm.c b/formats/format_wav_gsm.c index 69c5b695ae..7d0234fa26 100644 --- a/formats/format_wav_gsm.c +++ b/formats/format_wav_gsm.c @@ -475,13 +475,25 @@ static int wav_write(struct ast_filestream *s, struct ast_frame *f) static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence) { - off_t offset=0, distance, max; + off_t offset = 0, min = MSGSM_DATA_OFFSET, distance, max, cur; struct wavg_desc *s = (struct wavg_desc *)fs->_private; - off_t min = MSGSM_DATA_OFFSET; - off_t cur = ftello(fs->f); - fseek(fs->f, 0, SEEK_END); - max = ftello(fs->f); /* XXX ideally, should round correctly */ + if ((cur = ftello(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in WAV 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 WAV filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + + /* XXX ideally, should round correctly */ + if ((max = ftello(fs->f) < 0)) { + ast_log(AST_LOG_WARNING, "Unable to determine max position in WAV filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + /* Compute the distance in bytes, rounded to the block size */ distance = (sample_offset/MSGSM_SAMPLES) * MSGSM_FRAME_SIZE; if (whence == SEEK_SET) @@ -511,8 +523,21 @@ static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence) static int wav_trunc(struct ast_filestream *fs) { - if (ftruncate(fileno(fs->f), ftello(fs->f))) + int fd; + off_t cur; + + if ((fd = fileno(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for WAV 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 WAV 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); }