commit:     17e518250d3cabfec9ecc417275a42858b590297
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Wed Jan 31 20:39:24 2024 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Wed Jan 31 20:39:24 2024 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=17e51825

libq/contents: add variant specifying buffer length

This seems necessary for PR #21, but keep the original code structure
largely in-tact.

Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 libq/contents.c | 53 +++++++++++++++++++++++++++++++++++------------------
 libq/contents.h |  5 +++--
 2 files changed, 38 insertions(+), 20 deletions(-)

diff --git a/libq/contents.c b/libq/contents.c
index 7f4351d..feb1c0b 100644
--- a/libq/contents.c
+++ b/libq/contents.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2020 Gentoo Foundation
+ * Copyright 2005-2024 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  *
  * Copyright 2005-2008 Ned Ludd        - <[email protected]>
@@ -19,18 +19,20 @@
  * Parse a line of CONTENTS file and provide access to the individual fields
  */
 contents_entry *
-contents_parse_line(char *line)
+contents_parse_line_len(char *line, size_t len)
 {
        static contents_entry e;
        char *p;
 
-       if (line == NULL || *line == '\0' || *line == '\n')
+       if (len == 0 || line == NULL || *line == '\0' || *line == '\n')
                return NULL;
 
        /* chop trailing newline */
-       p = &line[strlen(line) - 1];
-       if (*p == '\n')
+       p = &line[len - 1];
+       if (*p == '\n') {
                *p = '\0';
+               len--;
+       }
 
        memset(&e, 0x00, sizeof(e));
        e._data = line;
@@ -53,23 +55,38 @@ contents_parse_line(char *line)
 
                /* obj /bin/bash 62ed51c8b23866777552643ec57614b0 1120707577 */
                case CONTENTS_OBJ:
-                       if ((e.mtime_str = strrchr(e.name, ' ')) == NULL)
-                               return NULL;
-                       *e.mtime_str++ = '\0';
-                       if ((e.digest = strrchr(e.name, ' ')) == NULL)
-                               return NULL;
-                       *e.digest++ = '\0';
+                       for (p = &e.name[len - 1]; p >= e.name; p--) {
+                               if (*p == ' ') {
+                                       if (e.mtime_str == NULL)
+                                               e.mtime_str = p + 1;
+                                       else if (e.digest == NULL)
+                                               e.digest = p + 1;
+                                       *p = '\0';
+
+                                       if (e.digest != NULL)
+                                               break;
+                               }
+                       }
                        break;
 
                /* sym /bin/sh -> bash 1120707577 */
                case CONTENTS_SYM:
-                       if ((e.mtime_str = strrchr(e.name, ' ')) == NULL)
-                               return NULL;
-                       *e.mtime_str++ = '\0';
-                       if ((e.sym_target = strstr(e.name, " -> ")) == NULL)
-                               return NULL;
-                       *e.sym_target = '\0';
-                       e.sym_target += 4;
+                       for (p = &e.name[len - 1]; p >= e.name; p--) {
+                               if (*p == ' ') {
+                                       if (e.mtime_str == NULL) {
+                                               e.mtime_str = p + 1;
+                                       } else if (e.sym_target == NULL) {
+                                               if (strncmp(p, " -> ", sizeof(" 
-> ") - 1) == 0)
+                                                       e.sym_target = p + 
sizeof(" -> ") - 1;
+                                               else
+                                                       continue;
+                                       }
+                                       *p = '\0';
+
+                                       if (e.sym_target != NULL)
+                                               break;
+                               }
+                       }
                        break;
        }
 

diff --git a/libq/contents.h b/libq/contents.h
index c766827..a0a5a63 100644
--- a/libq/contents.h
+++ b/libq/contents.h
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2019 Gentoo Foundation
+ * Copyright 2005-2024 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  *
  * Copyright 2005-2008 Ned Ludd        - <[email protected]>
@@ -24,6 +24,7 @@ typedef struct {
        long mtime;
 } contents_entry;
 
-contents_entry *contents_parse_line(char *line);
+contents_entry *contents_parse_line_len(char *line, size_t len);
+#define contents_parse_line(L) contents_parse_line_len(L, strlen(L))
 
 #endif

Reply via email to