summaryrefslogtreecommitdiff
path: root/formats
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
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')
-rw-r--r--formats/format_g719.c28
-rw-r--r--formats/format_g723.c15
-rw-r--r--formats/format_g729.c15
-rw-r--r--formats/format_gsm.c55
-rw-r--r--formats/format_h263.c15
-rw-r--r--formats/format_h264.c15
-rw-r--r--formats/format_ilbc.c15
-rw-r--r--formats/format_pcm.c65
-rw-r--r--formats/format_siren14.c29
-rw-r--r--formats/format_siren7.c29
-rw-r--r--formats/format_sln.c36
-rw-r--r--formats/format_vox.c66
-rw-r--r--formats/format_wav.c39
-rw-r--r--formats/format_wav_gsm.c37
14 files changed, 370 insertions, 89 deletions
diff --git a/formats/format_g719.c b/formats/format_g719.c
index aace0edc7..903fbcde9 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 7eba74147..b3e87367e 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 042977728..477883f17 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 03419c2f6..4cf6579a6 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 struct ast_format_def gsm_f = {
diff --git a/formats/format_h263.c b/formats/format_h263.c
index b00ae0fb3..ec08867d8 100644
--- a/formats/format_h263.c
+++ b/formats/format_h263.c
@@ -146,10 +146,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 155f46975..25ba54a77 100644
--- a/formats/format_h264.c
+++ b/formats/format_h264.c
@@ -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)
diff --git a/formats/format_ilbc.c b/formats/format_ilbc.c
index 7a08ca515..e18c78ab1 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 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);
}
diff --git a/formats/format_siren14.c b/formats/format_siren14.c
index 1ce50fad4..08c2cbfbf 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 2e5182d3c..955323ed2 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 1a73cdfbb..b90f6b27a 100644
--- a/formats/format_sln.c
+++ b/formats/format_sln.c
@@ -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)
diff --git a/formats/format_vox.c b/formats/format_vox.c
index 7d34df99c..bfa0d8e4c 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 78b3a7fa1..df09db353 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 35d4339f9..bbf3339bc 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);
}