[Home]

Summary:ASTERISK-13752: FILE function reads 1 character less than specified in length
Reporter:Bernd Casimir (bmc)Labels:
Date Opened:2009-03-15 13:46:46Date Closed:2009-03-16 12:38:52
Priority:MajorRegression?No
Status:Closed/CompleteComponents:Functions/func_env
Versions:Frequency of
Occurrence
Related
Issues:
Environment:Attachments:
Description:The replacement of ReadFile does not work as specified:

cat /tmp/test.txt
123456789

exten => s,n,ReadFile(VAR1=/tmp/test.txt,2);
exten => s,n,NoOp(VAR1=${VAR1});
exten => s,n,Set(VAR2=${FILE(/tmp/test.txt,0,2)});
exten => s,n,NoOp(VAR2=${VAR2});

   -- Executing [s@test:2] ReadFile("mISDN/3-u46", "VAR1=/tmp/test.txt,2") in new stack
   -- Executing [s@test:3] NoOp("mISDN/3-u46", "VAR1=12") in new stack
   -- Executing [s@test:4] Set("mISDN/3-u46", "VAR2=1") in new stack
   -- Executing [s@test:5] NoOp("mISDN/3-u46", "VAR2=1") in new stack



****** ADDITIONAL INFORMATION ******

The root cause of the problem is based on ast_copy_string defined in include/asterisk/strings.h:
"the destination buffer will always be null-terminated"

Therefore the null-termination overwrites the last element which should be copied...

I assume the problem can be fixed in funcs/func_env.c by replacing
ast_copy_string(buf, &contents[offset], length);
with
ast_copy_string(buf, &contents[offset], length+1);
but then other actions have to be taken that length+1 can never be larger than len!???
Comments:By: Leif Madsen (lmadsen) 2009-03-16 08:04:04

Assigned to Tilghman, as this type of issue appears to be right up his alley!

By: Digium Subversion (svnbot) 2009-03-16 12:33:39

Repository: asterisk
Revision: 182278

U   trunk/funcs/func_env.c

------------------------------------------------------------------------
r182278 | tilghman | 2009-03-16 12:33:39 -0500 (Mon, 16 Mar 2009) | 7 lines

Fix an off-by-one error in the FILE() function, and extend FILE()'s length parameter to work like variable substitution.
Previously, FILE() returned one less character than specified, due to the
terminating NULL.  Both the offset and length parameters now behave
identically to the way variable substitution offsets and lengths also work.
(closes issue ASTERISK-13752)
Reported by: BMC

------------------------------------------------------------------------

http://svn.digium.com/view/asterisk?view=rev&revision=182278

By: Digium Subversion (svnbot) 2009-03-16 12:36:52

Repository: asterisk
Revision: 182279

_U  branches/1.6.0/
U   branches/1.6.0/funcs/func_env.c

------------------------------------------------------------------------
r182279 | tilghman | 2009-03-16 12:36:52 -0500 (Mon, 16 Mar 2009) | 14 lines

Merged revisions 182278 via svnmerge from
https://origsvn.digium.com/svn/asterisk/trunk

........
 r182278 | tilghman | 2009-03-16 12:33:38 -0500 (Mon, 16 Mar 2009) | 7 lines
 
 Fix an off-by-one error in the FILE() function, and extend FILE()'s length parameter to work like variable substitution.
 Previously, FILE() returned one less character than specified, due to the
 terminating NULL.  Both the offset and length parameters now behave
 identically to the way variable substitution offsets and lengths also work.
 (closes issue ASTERISK-13752)
  Reported by: BMC
........

------------------------------------------------------------------------

http://svn.digium.com/view/asterisk?view=rev&revision=182279

By: Digium Subversion (svnbot) 2009-03-16 12:38:51

Repository: asterisk
Revision: 182280

_U  branches/1.6.1/
U   branches/1.6.1/channels/chan_local.c
U   branches/1.6.1/funcs/func_env.c

------------------------------------------------------------------------
r182280 | tilghman | 2009-03-16 12:38:51 -0500 (Mon, 16 Mar 2009) | 30 lines

Merged revisions 182211,182278 via svnmerge from
https://origsvn.digium.com/svn/asterisk/trunk

................
 r182211 | tilghman | 2009-03-16 10:50:55 -0500 (Mon, 16 Mar 2009) | 14 lines
 
 Merged revisions 182208 via svnmerge from
 https://origsvn.digium.com/svn/asterisk/branches/1.4
 
 ........
   r182208 | tilghman | 2009-03-16 10:39:15 -0500 (Mon, 16 Mar 2009) | 7 lines
   
   Fixup glare detection, to fix a memory leak of a local pvt structure.
   (closes issue ASTERISK-13738)
    Reported by: caspy
    Patches:
          20090313__bug14656__2.diff.txt uploaded by tilghman (license 14)
    Tested by: caspy
 ........
................
 r182278 | tilghman | 2009-03-16 12:33:38 -0500 (Mon, 16 Mar 2009) | 7 lines
 
 Fix an off-by-one error in the FILE() function, and extend FILE()'s length parameter to work like variable substitution.
 Previously, FILE() returned one less character than specified, due to the
 terminating NULL.  Both the offset and length parameters now behave
 identically to the way variable substitution offsets and lengths also work.
 (closes issue ASTERISK-13752)
  Reported by: BMC
................

------------------------------------------------------------------------

http://svn.digium.com/view/asterisk?view=rev&revision=182280