mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Re: [PATCH] syslog: Check result of connect function
       [not found] <20240321213109.10701-1-maks.mishinFZ@gmail.com>
@ 2024-03-21 22:34 ` Rich Felker
  0 siblings, 0 replies; only message in thread
From: Rich Felker @ 2024-03-21 22:34 UTC (permalink / raw)
  To: Maks Mishin; +Cc: musl

On Fri, Mar 22, 2024 at 12:31:09AM +0300, Maks Mishin wrote:
> Return value of of function 'connect', called at syslog.c:55,
> is not checked. The return value possibly contains
> an error code and ignoring it may lead to missing important errors.
> 
> Found by RASU JSC.
> 
> Signed-off-by: Maks Mishin <maks.mishinFZ@gmail.com>
> ---
>  src/misc/syslog.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/src/misc/syslog.c b/src/misc/syslog.c
> index 710202f9..72ad6d2c 100644
> --- a/src/misc/syslog.c
> +++ b/src/misc/syslog.c
> @@ -52,7 +52,12 @@ void closelog(void)
>  static void __openlog()
>  {
>  	log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
> -	if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
> +	if (log_fd >= 0) {
> +		int ret = connect(log_fd, (void *)&log_addr, sizeof log_addr);
> +		if (ret > 0) {
> +			errno = ret;
> +		}
> +	}
>  }
>  
>  void openlog(const char *ident, int opt, int facility)
> -- 
> 2.30.2

This patch is wrong but does nothing; connect is not permitted to
return positive values. It returns either 0 on success or -1 on error.

Since openlog cannot report failure to the caller (it returns void),
it's a best-effort operation. In the case where it fails to connect,
it's still desirable and intentional to leave a socket reserved so
that the program can't run out of file descriptors rendering it unable
to produce log output later. The lack of connection will be noticed
later when send() fails and the is_lost_conn predicate on errno
returns true.

Rich

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-03-21 22:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20240321213109.10701-1-maks.mishinFZ@gmail.com>
2024-03-21 22:34 ` [musl] Re: [PATCH] syslog: Check result of connect function Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).