]> 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:39:32 +0000 (19:39 +0000)
committerMatthew Jordan <mjordan@digium.com>
Mon, 16 Apr 2012 19:39:32 +0000 (19:39 +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/
........

Merged revisions 362151 from http://svn.asterisk.org/svn/asterisk/branches/1.8

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/10@362152 65c4cc65-6c06-0410-ace0-fbb531ad65f3

14 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_vox.c
formats/format_wav.c
formats/format_wav_gsm.c

index aace0edc739cd776e38415d53b304b817f2bdc25..903fbcde9810b430c97724b4129bec7109693a19 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 7eba74147561454214330d98d61db3fa4234de42..b3e87367e36353b5947ec6b7648f6db1711b2bd9 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 042977728c329230e6f53d7b76f7a0bc92dc75d6..477883f179ef94b9e222675fc1c8447896b85bbc 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 03419c2f6192c524507926cf3b96feec116d268d..4cf6579a607954be22afebbd52cdd98f92e6815a 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 struct ast_format_def gsm_f = {
index 77c1229c383ee4fc94edcc4237655f69b1f388da..1e820bf212ee08f809c933b5f98422dc02d68415 100644 (file)
@@ -147,10 +147,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 155f469759f215058a72dd8924970ab60264412b..25ba54a77c6fbb34b41989982e2bacc11456505d 100644 (file)
@@ -140,10 +140,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 7a08ca515c8a01554abc6834d61e8f1fda2125a5..e18c78ab1b6b3adfbd25956715948c0a1a12417c 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 3bf27388c627cc4ae83d60a5741f5d02630110c1..2a740ef5d08cc26f6b4a2910ea51b128ee12817d 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.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);
 }
 
index 1ce50fad4be28bc51ad2f72793e71a87de88bec8..08c2cbfbf7c10f21e70fadf5e2afd6c79794bbb7 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 2e5182d3c665fa5988f43b5a398bef04ce0ccf20..955323ed263b74169207f7b084ad46caa68ec075 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 1a73cdfbbf977bae3ae589aa9172454e488dea20..b90f6b27a277c5a84db280b03eaae0224596cb2c 100644 (file)
@@ -72,13 +72,25 @@ static int generic_write(struct ast_filestream *fs, struct ast_frame *f, enum as
 
 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)
@@ -95,7 +107,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 7d34df99cbda560aea6843d4499c2268acbf6443..bfa0d8e4cac6e55ccdac38b0ff3edb97c839f28a 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 78b3a7fa11e180f881fa8a294e961c58dbe524fa..df09db353a42b5a6d38f669a2242c3ff48b86df1 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 35d4339f9658f7fa46dda2ece09c3e0d677be359..bbf3339bc0ad2c2aabfa2e61202b084df310db06 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);
 }