]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
Check for IO stream failures in various format's truncate/seek operations
authorMatthew Jordan <mjordan@digium.com>
Mon, 16 Apr 2012 19:30:16 +0000 (19:30 +0000)
committerMatthew Jordan <mjordan@digium.com>
Mon, 16 Apr 2012 19:30:16 +0000 (19:30 +0000)
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

15 files changed:
formats/format_g719.c
formats/format_g723.c
formats/format_g729.c
formats/format_gsm.c
formats/format_h263.c
formats/format_h264.c
formats/format_ilbc.c
formats/format_pcm.c
formats/format_siren14.c
formats/format_siren7.c
formats/format_sln.c
formats/format_sln16.c
formats/format_vox.c
formats/format_wav.c
formats/format_wav_gsm.c

index d319fa8b49ef6199e2846abc246602b5c1eb35d2..692edad35b7f4b15dfc47804d5cb52727242efc4 100644 (file)
@@ -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)
index 7fe1a0a0c78ff6973f552943f9a9ec0e28f044a4..4d1c2cd56f1806d0719d5252e5783c43831e8305 100644 (file)
@@ -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)
index fcb7c1e6ac4c3a761f4a936965551b404cbb2f50..5863ccec83797ed5e73f90f03ca470cf1682c493 100644 (file)
@@ -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)
index 91225d3343f1a880e9e50f70b1fa13ac1b2dd764..e7aa24e27fcb5e4c743d17bd0be9a32e3766caf7 100644 (file)
@@ -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 = {
index 2de224a815928f1a258f8211504f362c33cd9cc0..757437c8a6ad8d9e14e3e828e683247bffaf22bf 100644 (file)
@@ -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)
index 84078d14fdab280f4ded182d48bea0f4be9d0cff..84e94aa7a57bedbca5f8d2161d64082827142056 100644 (file)
@@ -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)
index f620fd48e5e8dae5e059fcdad6ed1ca14a9369ae..b7673d8ef0fc36e124f76090a6e1925751c0db01 100644 (file)
@@ -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)
index 6985d2715bbbbcd9a22226b64a2b1c9b8afadaf5..9dfdaa81242ad0dea8afb49ff46841d52bbcce61 100644 (file)
@@ -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);
 }
 
index 0510800a48de07a192522aebbfdd43df77ab2cfb..067de7692308def78b283d2be2397853a6299a76 100644 (file)
@@ -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)
index 2a01e1b08d1a013818609a28589797331c12bce7..3a6b3b39c64a5b5ec5b2d2168dec5123d250420a 100644 (file)
@@ -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)
index 4227547e2df3b2c785f24e48bc8dd5ce43789954..d6c18d688b3aa174cdd86cf8b32210ae288cb9c3 100644 (file)
@@ -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)
index d999f687615b87ab81441955d81ec1d04b95a5e3..4cc099057ef81b3f7731e31e6105775b935f0a50 100644 (file)
@@ -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)
index eba299c486e422e3234b7e48bee4daf6f04c4ca4..a60808a11d7ac07dfdbf6b665c09061c685bf040 100644 (file)
@@ -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)
 {
index 2c1c3eca1a292bfe5b13453b09511cd02e799c16..c5e47ed19b84d01b4ea59347fd0f80717c1c3140 100644 (file)
@@ -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);
 }
 
index 69c5b695ae735038e15fcf7b53beecc66a244ef8..7d0234fa26d0ab0bfa60031194f9f5fc008b89fe 100644 (file)
@@ -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);
 }