]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
main/untils: Prevent potential infinite loop in ast_careful_fwrite
authorMatthew Jordan <mjordan@digium.com>
Thu, 3 Jul 2014 11:24:50 +0000 (11:24 +0000)
committerMatthew Jordan <mjordan@digium.com>
Thu, 3 Jul 2014 11:24:50 +0000 (11:24 +0000)
A loop in ast_careful_fwrite exists that will continually attempt to write to
a file stream, even in the presence of EAGAIN/EINTR errors. However, if a
connection that uses ast_careful_fwrite closes suddenly, ast_careful_fwrite's
call to fflush may return EAGAIN/EINTER along with EOF. A subsequent call to
fflush will return EOF but not clear errno, resulting in an infinite loop.

This patch clears errno after it is detected and handled the loop, such that
any subsequent call to fflush will not get erroneously stuck.

Review: https://reviewboard.asterisk.org/r/3704

#ASTERISK-23984 #close
Reported by: Steve Davies
patches:
  fflush_loop_fix uploaded by one47 (License 5012)
........

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

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

main/utils.c

index 7ae4e7620cc593ee0cbca35264e95f89c65aede5..e9b1607e2bce76946ab1e4d6dea342467e8c3879 100644 (file)
@@ -1402,11 +1402,18 @@ int ast_careful_fwrite(FILE *f, int fd, const char *src, size_t len, int timeout
                }
        }
 
+       errno = 0;
        while (fflush(f)) {
                if (errno == EAGAIN || errno == EINTR) {
+                       /* fflush() does not appear to reset errno if it flushes
+                        * and reaches EOF at the same time. It returns EOF with
+                        * the last seen value of errno, causing a possible loop.
+                        * Also usleep() to reduce CPU eating if it does loop */
+                       errno = 0;
+                       usleep(1);
                        continue;
                }
-               if (!feof(f)) {
+               if (errno && !feof(f)) {
                        /* Don't spam the logs if it was just that the connection is closed. */
                        ast_log(LOG_ERROR, "fflush() returned error: %s\n", strerror(errno));
                }