commit:     17c3a565b626fbace414fae18c733dc578263824
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Mon Apr  2 15:41:41 2018 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Mon Apr  2 15:41:41 2018 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=17c3a565

qatom: print unset members as <unset> instead of relying on libc

Some libcs do NOT do the favour of turning a NULL-pointer into the
string "(null)", but segfault instead.  This is just a friendly guesture
of libcs which do, but it is nowhere standard or that this should result
in "(null)".  While "(null)" is actually a developer concept, and
possibly out of context for users, check for printing NULL-pointers and
replace them with "<unset>" instead.

 qatom.c            | 20 ++++++++++++--------
 tests/qatom/dotest |  6 +++---
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/qatom.c b/qatom.c
index 7f981c3..b542de8 100644
--- a/qatom.c
+++ b/qatom.c
@@ -59,21 +59,22 @@ qatom_printf(const char *format, const depend_atom *atom, 
int pverbose)
                        if (p) {
                                size_t len = p - fmt;
                                bool showit = (bracket == '{') || pverbose;
+#define HN(X) (X ? X : "<unset>")
                                if (!strncmp("CATEGORY", fmt, len)) {
                                        if (showit || atom->CATEGORY)
-                                               printf("%s", atom->CATEGORY);
+                                               printf("%s", 
HN(atom->CATEGORY));
                                } else if (!strncmp("P", fmt, len)) {
                                        if (showit || atom->P)
-                                               printf("%s", atom->P);
+                                               printf("%s", HN(atom->P));
                                } else if (!strncmp("PN", fmt, len)) {
                                        if (showit || atom->PN)
-                                               printf("%s", atom->PN);
+                                               printf("%s", HN(atom->PN));
                                } else if (!strncmp("PV", fmt, len)) {
                                        if (showit || atom->PV)
-                                               printf("%s", atom->PV);
+                                               printf("%s", HN(atom->PV));
                                } else if (!strncmp("PVR", fmt, len)) {
                                        if (showit || atom->PVR)
-                                               printf("%s", atom->PVR);
+                                               printf("%s", HN(atom->PVR));
                                } else if (!strncmp("PF", fmt, len)) {
                                        printf("%s", atom->PN);
                                        if (atom->PV)
@@ -85,16 +86,19 @@ qatom_printf(const char *format, const depend_atom *atom, 
int pverbose)
                                                printf("r%i", atom->PR_int);
                                } else if (!strncmp("SLOT", fmt, len)) {
                                        if (showit || atom->SLOT)
-                                               printf(":%s", atom->SLOT ? : 
"-");
+                                               printf(":%s", atom->SLOT ? 
atom->SLOT : "-");
                                } else if (!strncmp("pfx", fmt, len)) {
                                        if (showit || atom->pfx_op != 
ATOM_OP_NONE)
-                                               fputs(atom->pfx_op == 
ATOM_OP_NONE ? "-" : atom_op_str[atom->pfx_op], stdout);
+                                               fputs(atom->pfx_op == 
ATOM_OP_NONE ?
+                                                               "-" : 
atom_op_str[atom->pfx_op], stdout);
                                } else if (!strncmp("sfx", fmt, len)) {
                                        if (showit || atom->sfx_op != 
ATOM_OP_NONE)
-                                               fputs(atom->sfx_op == 
ATOM_OP_NONE ? "-" : atom_op_str[atom->sfx_op], stdout);
+                                               fputs(atom->sfx_op == 
ATOM_OP_NONE ?
+                                                               "-" : 
atom_op_str[atom->sfx_op], stdout);
                                } else
                                        printf("<BAD:%.*s>", (int)len, fmt);
                                ++p;
+#undef HN
                        } else
                                p = fmt + 1;
                } else

diff --git a/tests/qatom/dotest b/tests/qatom/dotest
index c14001f..4bb0460 100755
--- a/tests/qatom/dotest
+++ b/tests/qatom/dotest
@@ -19,8 +19,8 @@ test() {
 }
 
 # Legacy format.
-test l01 "(null) pkg (null)" "pkg"
-test l02 "cat pkg (null)" "cat/pkg"
+test l01 "<unset> pkg <unset>" "pkg"
+test l02 "cat pkg <unset>" "cat/pkg"
 test l03 "cat pkg 123" "cat/pkg-123"
 test l04 "cat pkg 123 r4" "cat/pkg-123-r4"
 test l05 "cat pkg 123 r4 :5" "cat/pkg-123-r4:5"
@@ -30,7 +30,7 @@ test l07 "cat pkg 123 = *" "=cat/pkg-123*"
 
 # Explicit format.
 test f01 "cat"    -F '%{CATEGORY}' "cat/pkg"
-test f02 "(null)" -F '%{CATEGORY}' "pkg"
+test f02 "<unset>" -F '%{CATEGORY}' "pkg"
 test f03 ""       -F '%[CATEGORY]' "pkg"
 test f04 "cat"        -F '%{CATEGORY}' "cat/pkg-123-r4:5"
 test f05 "pkg-123"    -F '%{P}'        "cat/pkg-123-r4:5"

Reply via email to