]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-remote: replace extract_first_word() with simple strchr()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 11 Feb 2025 07:36:12 +0000 (16:36 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 16 Feb 2025 11:08:39 +0000 (20:08 +0900)
src/journal-remote/journal-compression-util.c
src/journal-remote/journal-upload.c

index 4def84b2bd79338129d51f3d5b7cf9d226b10c8e..80710d6fec8ec4e742f289f29cbbccc23fdf3270 100644 (file)
@@ -36,7 +36,7 @@ int config_parse_compression(
         }
 
         for (const char *p = rvalue;;) {
-                _cleanup_free_ char *algorithm = NULL, *word = NULL;
+                _cleanup_free_ char *word = NULL;
                 int level = -1;
 
                 r = extract_first_word(&p, &word, NULL, 0);
@@ -46,25 +46,23 @@ int config_parse_compression(
                         return 1;
 
                 if (parse_level) {
-                        const char *q = word;
-                        r = extract_first_word(&q, &algorithm, ":", 0);
-                        if (r < 0)
-                                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
-                        if (!isempty(q)) {
+                        char *q = strchr(word, ':');
+                        if (q) {
+                                *q++ = '\0';
+
                                 r = safe_atoi(q, &level);
                                 if (r < 0) {
                                         log_syntax(unit, LOG_WARNING, filename, line, r,
-                                                   "Compression level %s should be positive, ignoring.", q);
+                                                   "Compression level must be positive, ignoring: %s", q);
                                         continue;
                                 }
                         }
-                } else
-                        algorithm = TAKE_PTR(word);
+                }
 
-                Compression c = compression_lowercase_from_string(algorithm);
+                Compression c = compression_lowercase_from_string(word);
                 if (c < 0 || !compression_supported(c)) {
                         log_syntax(unit, LOG_WARNING, filename, line, c,
-                                   "Compression=%s is not supported on a system, ignoring.", algorithm);
+                                   "Compression algorithm '%s' is not supported on the system, ignoring.", word);
                         continue;
                 }
 
index 15704c4396c33ce6e63d2c3cd4daa2dfae358984..05ba7a6989cbf29ae265e3578715762478ed718b 100644 (file)
@@ -503,28 +503,25 @@ static int update_content_encoding(Uploader *u, const char *accept_encoding) {
         assert(u);
 
         for (const char *p = accept_encoding;;) {
-                _cleanup_free_ char *encoding_value = NULL, *alg = NULL;
-                Compression algorithm;
-                CURLcode code;
+                _cleanup_free_ char *word = NULL;
 
-                r = extract_first_word(&p, &encoding_value, ",", 0);
+                r = extract_first_word(&p, &word, ",", 0);
                 if (r < 0)
-                        return log_error_errno(r, "Failed to extract Accept-Encoding header value: %m");
+                        return log_error_errno(r, "Failed to parse Accept-Encoding header value: %m");
                 if (r == 0)
                         return 0;
 
-                const char *q = encoding_value;
-                r = extract_first_word(&q, &alg, ";", 0);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to extract compression algorithm from Accept-Encoding header: %m");
+                /* Cut the quality value waiting. */
+                char *q = strchr(word, ';');
+                if (q)
+                        *q = '\0';
 
-                algorithm = compression_lowercase_from_string(alg);
-                if (algorithm <= 0 || !compression_supported(algorithm)) {
-                        continue;
-                }
+                Compression c = compression_lowercase_from_string(word);
+                if (c <= 0 || !compression_supported(c))
+                        continue; /* unsupported or invalid algorithm. */
 
                 FOREACH_ARRAY(opt, arg_compression.opts, arg_compression.size) {
-                        if (opt->algorithm != algorithm)
+                        if (opt->algorithm != c)
                                 continue;
 
                         _cleanup_free_ char *header = strjoin("Content-Encoding: ", compression_lowercase_to_string(u->compression.algorithm));
@@ -548,6 +545,7 @@ static int update_content_encoding(Uploader *u, const char *accept_encoding) {
                                 u->header = l;
                         }
 
+                        CURLcode code;
                         easy_setopt(u->easy, CURLOPT_HTTPHEADER, u->header, LOG_ERR, return -EXFULL);
                         u->compression = *opt;
                         return 0;