Introduce several more helper functions to read sysfs attributes and use them where applicable (#41065)

Let's not use sd_device_get_sysattr_value() unless the result is
sanitized, escaped, or filtered.
This commit is contained in:
Yu Watanabe
2026-05-18 07:26:44 +09:00
committed by GitHub
29 changed files with 393 additions and 396 deletions
+1
View File
@@ -103,6 +103,7 @@ typedef enum Glyph Glyph;
typedef enum ImageClass ImageClass;
typedef enum JobMode JobMode;
typedef enum RuntimeScope RuntimeScope;
typedef enum StringSafeFlags StringSafeFlags;
typedef enum TimestampStyle TimestampStyle;
typedef enum UnitActiveState UnitActiveState;
typedef enum UnitDependency UnitDependency;
+5 -1
View File
@@ -1113,7 +1113,11 @@ bool string_is_safe(const char *p, StringSafeFlags flags) {
return false;
for (const char *t = p; *t; t++) {
if ((*t > 0 && *t < ' ') || *t == 0x7f) /* never allow control characters */
/* never allow control characters, except for new line */
if ((*t > 0 && *t < ' ' && *t != '\n') || *t == 0x7f)
return false;
if (!FLAGS_SET(flags, STRING_ALLOW_NEWLINES) && *t == '\n')
return false;
if (!FLAGS_SET(flags, STRING_ALLOW_BACKSLASHES) && *t == '\\')
+5 -4
View File
@@ -223,10 +223,11 @@ static inline int strdup_to(char **ret, const char *src) {
typedef enum StringSafeFlags {
STRING_ASCII = 1 << 0, /* Verify string is 7-Bit ASCII (rather than just UTF-8) */
STRING_ALLOW_EMPTY = 1 << 1, /* Allow empty strings */
STRING_ALLOW_BACKSLASHES = 1 << 2, /* Allow backslashes (\) */
STRING_ALLOW_QUOTES = 1 << 3, /* Allow quotes (" or ') */
STRING_ALLOW_GLOBS = 1 << 4, /* Allow globs (?, * or [) */
STRING_FILENAME = 1 << 5, /* Verify the string is valid as regular filename */
STRING_ALLOW_NEWLINES = 1 << 2, /* Allow newlines (\n) */
STRING_ALLOW_BACKSLASHES = 1 << 3, /* Allow backslashes (\) */
STRING_ALLOW_QUOTES = 1 << 4, /* Allow quotes (" or ') */
STRING_ALLOW_GLOBS = 1 << 5, /* Allow globs (?, * or [) */
STRING_FILENAME = 1 << 6, /* Verify the string is valid as regular filename */
} StringSafeFlags;
bool string_is_safe(const char *p, StringSafeFlags flags) _pure_;