Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
375e349
lib/typetraits.h: QChar_of(): Add macro
alejandro-colomar Oct 19, 2025
1ecc403
lib/atoi/: a2i(): Re-implement with a statement expression
alejandro-colomar Oct 16, 2025
7374c78
lib/atoi/, */: Move all a2i() macros to the same file
alejandro-colomar Oct 16, 2025
f9bb612
lib/atoi/, */: Move all str2i() macros together with a2i()
alejandro-colomar Oct 17, 2025
4437848
lib/cast.h: const_cast(): Pass through other types
alejandro-colomar Oct 18, 2025
111b468
lib/atoi/a2i.h: Use const_cast() instead of a raw cast
alejandro-colomar Oct 18, 2025
8d9343f
lib/, src/: LFIND(), LSEARCH(), QSORT(): Pass the type explicitly
alejandro-colomar Mar 12, 2025
70894b8
lib/search/l/: LFIND(), LSEARCH(): Tighten return values
alejandro-colomar Mar 13, 2025
06521bf
lib/search/: CMP(): Remove indirection to simplify
alejandro-colomar Mar 13, 2025
9b3e427
lib/search/cmp/: CMP(): Use a function literal to reduce repetition
alejandro-colomar Mar 12, 2025
3289a00
lib/search/l/: LFIND(): Use a function literal to add type safety
alejandro-colomar Mar 12, 2025
2bbac4c
lib/search/l/: LSEARCH(): Use a function literal to add type safety
alejandro-colomar Mar 13, 2025
9710f47
lib/search/sort/: QSORT(): Use a function literal to add type safety
alejandro-colomar Mar 13, 2025
e08b62f
lib/atoi/, *: Use function literals to avoid repetition
alejandro-colomar Mar 10, 2025
124546f
lib/atoi/a2i.h: Use function literal to add robustness
alejandro-colomar Oct 28, 2025
4c80e45
lib/alloc/: Use function literals to add safety
alejandro-colomar Oct 28, 2025
47fbca5
lib/adds.h: Calculate array size in a macro
alejandro-colomar Mar 12, 2025
c3c6ede
lib/adds.h: Use compound literal to simplify
alejandro-colomar Mar 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 4 additions & 22 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,12 @@ libshadow_la_SOURCES = \
alloc/realloc.h \
alloc/reallocf.c \
alloc/reallocf.h \
atoi/a2i/a2i.c \
atoi/a2i/a2i.h \
atoi/a2i/a2s.c \
atoi/a2i/a2s.h \
atoi/a2i/a2s_c.c \
atoi/a2i/a2s_c.h \
atoi/a2i/a2s_nc.c \
atoi/a2i/a2s_nc.h \
atoi/a2i/a2u.c \
atoi/a2i/a2u.h \
atoi/a2i/a2u_c.c \
atoi/a2i/a2u_c.h \
atoi/a2i/a2u_nc.c \
atoi/a2i/a2u_nc.h \
atoi/a2i.c \
atoi/a2i.h \
atoi/getnum.c \
atoi/getnum.h \
atoi/str2i.c \
atoi/str2i.h \
atoi/strtoi/strtoi.c \
atoi/strtoi/strtoi.h \
atoi/strtoi/strtou.c \
atoi/strtoi/strtou.h \
atoi/strtoi/strtou_noneg.c \
atoi/strtoi/strtou_noneg.h \
atoi/strton.c \
atoi/strton.h \
attr.h \
audit_help.c \
basename.c \
Expand Down
2 changes: 1 addition & 1 deletion lib/addgrps.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ add_groups(const char *list)
continue;
}

LSEARCH(&grp->gr_gid, gids, &n);
LSEARCH(gid_t, &grp->gr_gid, gids, &n);
}
free(dup);

Expand Down
13 changes: 6 additions & 7 deletions lib/adds.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
#include "sizeof.h"


#define addsl(a, b, ...) \
({ \
long addend_[] = {a, b, __VA_ARGS__}; \
\
addslN(countof(addend_), addend_); \
})
// addsl - add with-saturation long
#define addsl(a, b, ...) addslN_a(((long []){a, b, __VA_ARGS__}))


#define addslN_a(a) addslN(countof(a), a)


inline long addsl2(long a, long b);
Expand Down Expand Up @@ -55,7 +54,7 @@ addslN(size_t n, long addend[n])

e = errno;
while (n > 1) {
QSORT(addend, n);
QSORT(long, addend, n);

errno = 0;
addend[0] = addsl2(addend[0], addend[--n]);
Expand Down
9 changes: 5 additions & 4 deletions lib/alloc/calloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
#include "exit_if_null.h"


#define CALLOC(n, type) \
( \
(type *) calloc(n, sizeof(type)) \
)
#define CALLOC(n_, T) \
((static inline typeof(T) *(size_t n)) \
{ \
return calloc(n, sizeof(T)); \
}(n_)


#define XCALLOC(n, type) exit_if_null(CALLOC(n, type))
Expand Down
9 changes: 5 additions & 4 deletions lib/alloc/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
#include "exit_if_null.h"


#define MALLOC(n, type) \
( \
(type *) mallocarray(n, sizeof(type)) \
)
#define MALLOC(n_, T) \
((static inline typeof(T) *(size_t n)) \
{ \
return mallocarray(n, sizeof(T)); \
}(n_))


#define XMALLOC(n, type) exit_if_null(MALLOC(n, type))
Expand Down
9 changes: 5 additions & 4 deletions lib/alloc/realloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
#include "exit_if_null.h"


#define REALLOC(p, n, type) \
( \
_Generic(p, type *: (type *) reallocarray(p, (n) ?: 1, sizeof(type))) \
)
#define REALLOC(p_, n_, T) \
((static inline typeof(T) *(typeof(T) *p, size_t n)) \
{ \
return reallocarray(p, n ?: 1, sizeof(T)); \
}(p_, n_))


#define XREALLOC(p, n, type) exit_if_null(REALLOC(p, n, type))
Expand Down
9 changes: 5 additions & 4 deletions lib/alloc/reallocf.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
#include "attr.h"


#define REALLOCF(p, n, type) \
( \
_Generic(p, type *: (type *) reallocarrayf(p, (n) ?: 1, sizeof(type)))\
)
#define REALLOCF(p_, n_, T) \
((static inline typeof(T) *(typeof(T) *p, size_t n)) \
{ \
return reallocarrayf(p, n ?: 1, sizeof(T)); \
}(p_, n_))


ATTR_ALLOC_SIZE(2, 3)
Expand Down
3 changes: 1 addition & 2 deletions lib/atoi/str2i.c → lib/atoi/a2i.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-FileCopyrightText: 2007-2009, Nicolas François
// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#include "atoi/str2i.h"
#include "atoi/a2i.h"
74 changes: 74 additions & 0 deletions lib/atoi/a2i.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_ATOI_A2I_H_
#define SHADOW_INCLUDE_LIB_ATOI_A2I_H_


#include "config.h"

#include <errno.h>
#include <stddef.h>

#include "atoi/strton.h"
#include "cast.h"
#include "typetraits.h"


/*
* See the manual of these macros in liba2i's documentation:
* <http://www.alejandro-colomar.es/share/dist/liba2i/git/HEAD/liba2i-HEAD.pdf>
*/


#define a2i_(T, QChar, ...) \
((static inline int \
(T *n, QChar *s, QChar **endp, int base, T min, T max)) \
{ \
int status; \
\
*n = _Generic(T, \
short: strtoi_, \
int: strtoi_, \
long: strtoi_, \
long long: strtoi_, \
unsigned short: strtou_noneg, \
unsigned int: strtou_noneg, \
unsigned long: strtou_noneg, \
unsigned long long: strtou_noneg \
)(s, endp, base, min, max, &status); \
\
if (status != 0) \
errno = status; \
return -!!status; \
}(__VA_ARGS__))


// a2i - alpha to integer
#define a2i(T, n, s, ...) a2i_(T, QChar_of(s), n, s, __VA_ARGS__)

#define a2sh(...) a2i(short, __VA_ARGS__)
#define a2si(...) a2i(int, __VA_ARGS__)
#define a2sl(...) a2i(long, __VA_ARGS__)
#define a2sll(...) a2i(long long, __VA_ARGS__)

#define a2uh(...) a2i(unsigned short, __VA_ARGS__)
#define a2ui(...) a2i(unsigned int, __VA_ARGS__)
#define a2ul(...) a2i(unsigned long, __VA_ARGS__)
#define a2ull(...) a2i(unsigned long long, __VA_ARGS__)

#define str2i(T, ...) a2i(T, __VA_ARGS__, NULL, 0, type_min(T), type_max(T))

#define str2sh(...) str2i(short, __VA_ARGS__)
#define str2si(...) str2i(int, __VA_ARGS__)
#define str2sl(...) str2i(long, __VA_ARGS__)
#define str2sll(...) str2i(long long, __VA_ARGS__)

#define str2uh(...) str2i(unsigned short, __VA_ARGS__)
#define str2ui(...) str2i(unsigned int, __VA_ARGS__)
#define str2ul(...) str2i(unsigned long, __VA_ARGS__)
#define str2ull(...) str2i(unsigned long long, __VA_ARGS__)


#endif // include guard
7 changes: 0 additions & 7 deletions lib/atoi/a2i/a2i.c

This file was deleted.

62 changes: 0 additions & 62 deletions lib/atoi/a2i/a2i.h

This file was deleted.

7 changes: 0 additions & 7 deletions lib/atoi/a2i/a2s.c

This file was deleted.

20 changes: 0 additions & 20 deletions lib/atoi/a2i/a2s.h

This file was deleted.

17 changes: 0 additions & 17 deletions lib/atoi/a2i/a2s_c.c

This file was deleted.

Loading
Loading