9
0
Fork 0

Fix some places in library where semaphore is not released on error conditions

git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@5071 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
patacongo 2012-08-31 16:03:17 +00:00
parent 2848bbca14
commit adff605b65
28 changed files with 152 additions and 81 deletions

View File

@ -295,3 +295,6 @@
configuration variable CONFIG_USER_ENTRYPOINT that may be used to change
the default entry from user_start to some other symbol. Contributed by
Kate.
* apps/netutils/webserver/httpd/c: Fix a typo that as introduced in
version r4402: 'lese' instead of 'else' (Noted by Max Holtzberg).

View File

@ -380,7 +380,7 @@ static inline int httpd_cmd(struct httpd_state *pstate)
ndbg("[%d] recv failed: %d\n", pstate->ht_sockfd, errno);
return ERROR;
}
lese if (recvlen == 0)
else if (recvlen == 0)
{
ndbg("[%d] connection lost\n", pstate->ht_sockfd);
return ERROR;

View File

@ -3224,4 +3224,14 @@
* sched/os_bring.c, configs/*/defconfig, tools/mkconfig.c, and others: Added
configuration variable CONFIG_USER_ENTRYPOINT that may be used to change
the default entry from user_start to some other symbol. Contributed by
Kate.
Kate. NOTE: This change does introduce a minor backward incompatibility.
For example, if your application uses NSH as its start-up program, then your
code will not fail because it will be unable to find "user_start". The fix
for this link failure is to add the following to your configuration file:
CONFIG_USER_ENTRYPOINT="nsh_main".
* libs/stdio/lib_libfread.c and lib_*flush*.c: Correct a couple of
error cases where the lib semaphore was not be released on error
exits (thanks Ronen Vainish). Also, improved some error reporting:
the generic ERROR was being used instead of the specific errno
value; the errno variable was not always set correctly.

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_fflush.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -97,22 +97,36 @@
int fflush(FAR FILE *stream)
{
int ret;
/* Is the stream argument NULL? */
if (!stream)
{
/* Yes... then this is a request to flush all streams */
return lib_flushall(sched_getstreams());
ret = lib_flushall(sched_getstreams());
}
else if (lib_fflush(stream, true) != 0)
else
{
/* An error occurred during the flush AND/OR we were unable to flush all
* of the buffered write data. Return EOF on failure.
ret = lib_fflush(stream, true);
}
/* Check the return value */
if (ret < 0)
{
/* An error occurred during the flush AND/OR we were unable to flush
* all of the buffered write data. Set the errno value.
*/
set_errno(-ret);
/* And return EOF on failure. */
return EOF;
}
return OK;
}

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_fgetc.c
*
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_fgetpos.c
*
* Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_fileno.c
*
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_fread.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_fseek.c
*
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_fsetpos.c
*
* Copyright (C) 2008, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_ftell.c
*
* Copyright (C) 2008, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_fwrite.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -98,8 +98,8 @@
* bforce - flush must be complete.
*
* Return:
* ERROR on failure, otherwise the number of bytes remaining in the buffer.
* If bforce is set, then only the values ERROR and 0 will be returned.
* A negated errno value on failure, otherwise the number of bytes remaining
* in the buffer.
*
****************************************************************************/
@ -109,13 +109,13 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce)
FAR const unsigned char *src;
ssize_t bytes_written;
ssize_t nbuffer;
int ret;
/* Return EBADF if the file is not opened for writing */
if (stream->fs_filedes < 0 || (stream->fs_oflags & O_WROK) == 0)
{
set_errno(EBADF);
return ERROR;
return -EBADF;
}
/* Make sure that we have exclusive access to the stream */
@ -132,8 +132,11 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce)
if (stream->fs_bufread != stream->fs_bufstart)
{
/* The buffer holds read data... just return zero */
/* The buffer holds read data... just return zero meaning "no bytes
* remaining in the buffer."
*/
lib_give_semaphore(stream);
return 0;
}
@ -151,8 +154,12 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce)
bytes_written = write(stream->fs_filedes, src, nbuffer);
if (bytes_written < 0)
{
/* Write failed. The cause of the failure is in 'errno'.
* returned the negated errno value.
*/
lib_give_semaphore(stream);
return ERROR;
return -get_errno();
}
/* Handle partial writes. fflush() must either return with

View File

@ -1,8 +1,8 @@
/****************************************************************************
* lib/stdio/lib_libflushall.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -91,7 +91,7 @@
int lib_flushall(FAR struct streamlist *list)
{
int lasterrno = OK;
int ret = OK;
int ret;
/* Make sure that there are streams associated with this thread */
@ -115,25 +115,23 @@ int lib_flushall(FAR struct streamlist *list)
{
/* Flush the writable FILE */
if (lib_fflush(stream, true) != 0)
ret = lib_fflush(stream, true);
if (ret < 0)
{
/* An error occurred during the flush AND/OR we were unable
* to flush all of the buffered write data. Return EOF on failure.
* to flush all of the buffered write data. Remember the
* last errcode.
*/
lasterrno = get_errno();
ret = ERROR;
lasterrno = ret;
}
}
}
stream_semgive(list);
}
/* If any flush failed, return that last failed flush */
/* If any flush failed, return the errorcode of the last failed flush */
if (ret != OK)
{
set_errno(lasterrno);
}
return ret;
return lasterrno;
}

View File

@ -1,8 +1,8 @@
/****************************************************************************
* lib/stdio/lib_libfread.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -88,6 +88,7 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
{
unsigned char *dest = (unsigned char*)ptr;
ssize_t bytes_read;
int ret;
/* Make sure that reading from this stream is allowed */
@ -127,9 +128,11 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
* buffered read/write access.
*/
if (lib_wrflush(stream) != 0)
ret = lib_wrflush(stream);
if (ret < 0)
{
return ERROR;
lib_give_semaphore(stream);
return ret;
}
/* Now get any other needed chars from the buffer or the file. */
@ -176,15 +179,17 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
bytes_read = read(stream->fs_filedes, dest, count);
if (bytes_read < 0)
{
/* An error occurred on the read */
/* An error occurred on the read. The error code is
* in the 'errno' variable.
*/
goto err_out;
goto errout_with_errno;
}
else if (bytes_read == 0)
{
/* We are at the end of the file */
goto short_read;
goto shortread;
}
else
{
@ -198,7 +203,7 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
{
/* No. We must be at the end of file. */
goto short_read;
goto shortread;
}
else
{
@ -219,15 +224,17 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
bytes_read = read(stream->fs_filedes, stream->fs_bufread, buffer_available);
if (bytes_read < 0)
{
/* An error occurred on the read */
/* An error occurred on the read. The error code is
* in the 'errno' variable.
*/
goto err_out;
goto errout_with_errno;
}
else if (bytes_read == 0)
{
/* We are at the end of the file */
goto short_read;
goto shortread;
}
else
{
@ -246,7 +253,11 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
bytes_read = read(stream->fs_filedes, dest, count);
if (bytes_read < 0)
{
goto err_out;
/* An error occurred on the read. The error code is
* in the 'errno' variable.
*/
goto errout_with_errno;
}
else if (bytes_read == 0)
{
@ -259,13 +270,21 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
}
}
#endif
/* Here after a successful (but perhaps short) read */
#if CONFIG_STDIO_BUFFER_SIZE > 0
short_read:
shortread:
#endif
bytes_read = dest - (unsigned char*)ptr;
err_out:
lib_give_semaphore(stream);
}
return bytes_read;
lib_give_semaphore(stream);
return bytes_read;
/* Error exits */
errout_with_errno:
lib_give_semaphore(stream);
return -get_errno();
}

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_libsprintf.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_rdflush.c
*
* Copyright (C) 2008, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_snprintf.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_sprintf.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_sscanf.c
*
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_syslogstream.c
*
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_ungetc.c
*
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_vfprintf.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_vprintf.c
*
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_vsnprintf.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_vsprintf.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -1,8 +1,8 @@
/****************************************************************************
* lib/stdio/lib_wrflush.c
*
* Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Copyright (C) 2008-2009, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -88,29 +88,49 @@
int lib_wrflush(FAR FILE *stream)
{
#if CONFIG_STDIO_BUFFER_SIZE > 0
int ret;
/* Verify that we were passed a valid (i.e., non-NULL) stream */
#if CONFIG_STDIO_BUFFER_SIZE > 0
if (stream)
#ifdef CONFIG_DEBUG
if (!stream)
{
/* Verify that the stream is opened for writing... lib_fflush will
* return an error if it is called for a stream that is not opened for
* writing.
return -EINVAL;
}
#endif
/* Verify that the stream is opened for writing... lib_fflush will
* return an error if it is called for a stream that is not opened for
* writing. Check that first so that this function will not fail in
* that case.
*/
if ((stream->fs_oflags & O_WROK) == 0)
{
/* Report that the success was successful if we attempt to flush a
* read-only stream.
*/
if ((stream->fs_oflags & O_WROK) == 0 ||
lib_fflush(stream, true) == 0)
{
/* Return success if there is no buffered write data -- i.e., that
* the stream is not opened for writing or, if it is, that all of
* the buffered write data was successfully flushed.
*/
return OK;
}
return OK;
}
return ERROR;
/* Flush the stream. Return success if there is no buffered write data
* -- i.e., that the stream is opened for writing and that all of the
* buffered write data was successfully flushed by lib_fflush().
*/
return lib_fflush(stream, true);
#else
return stream ? OK : ERROR;
/* Verify that we were passed a valid (i.e., non-NULL) stream */
#ifdef CONFIG_DEBUG
if (!stream)
{
return -EINVAL;
}
#endif
return OK;
#endif
}

View File

@ -2,7 +2,7 @@
* lib/stdio/lib_zeroinstream.c
*
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions