Skip to content

Conversation

@alejandro-colomar
Copy link
Collaborator

Queued after #1289 .

@alejandro-colomar alejandro-colomar changed the title Simplify error handling Simplify error handling (especially, error messages) Jul 26, 2025
@alejandro-colomar alejandro-colomar changed the title Simplify error handling (especially, error messages) Simplify error handling (mainly, error messages) Jul 26, 2025
The double parentheses were used to fake a variadic macro with a
non-variadic one.  With a variadic macro, we remove the weird double
parentheses that were needed to call this macro, which BTW were
error-prone.  This would have prevented the bug fixed in 3f5ae5d
(2025-09-10; "src/su.c: Fix incorrect (non-matching) parentheses").

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
The name of this macro makes it more evident what it does.  It's a
C-locale version of syslog(3).  We need to implement it as a macro
so that arguments such as strerror(3) are evaluated after setting
the C locale.  This would be impossible with a function.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
There's no need for two variables, and this avoids one assignment.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
…nally

Both setlocale(3) and free(3) are okay to call with a null pointer.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
In some cases, we print strerrno() with this macro.  Because this is
a macro, and internal calls such as strdup(3) may set errno, that value
could be corrupted when we arrive at syslog(3).  While we could solve
this here, it's not robust.  Instead, we'll use a dedicated wrapper for
that, which will be added in the following commits: SYSLOGE().

What we'll do here is preserve errno when we exit from this macro, as we
often follow SYSLOG() calls with fprintf(stderr,) calls, which also use
the errno value, and we don't want to pollute that.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
These functions print a formatted string, followed by a colon and
a space, followed by an error string produced by strerror($2).  That is,
the output is as follows:

	fmt: error message string

For example,
	fprintec(stderr, ENOMEM, "foo(%d)", 42);
prints
	foo(42): Cannot allocate memory

These functions return the number of characters printed, or a negative
value on error, like fprintf(3).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
fprinte() is like fprintec(), but uses errno instead of an error code.

It is implemented as a macro so that it can read the value in errno
before evaluating any of its arguments, thus not corrupting it.

It also preserves errno so that the error can be printed more than once.
This is useful because we often print to stderr or log_get_logfd(), and
immediately after print to the system log with syslog(3).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
like the fprintf(3) wrappers, these print an error message.  SYSLOGE()
uses errno, and SYSLOGEC() uses an errno-like error code.

This time we need to be careful to name the local copy of errno
differently than within SYSLOG() --which we call--.  Let's use a double
underscore.  In the future, C might have function literals (similar to
lambdas), which will solve this issue.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Having such long and complex format strings and variadic arguments is
error-prone, as can be seen in the previous commit, which fixes a bug of
this kind.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This will allow having shorter lines for writing to stderr.

This name is commonly used in other projects, it seems (see link below).

Link: <https://gcc.gnu.org/onlinedocs/gcc-3.1.1/cpp/Variadic-Macros.html>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Print simpler messages that don't need translation.
Check libc errors with ==-1 and ==NULL instead of <0 (or !=0) and !p.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant