<pre style='margin:0'>
Zero King (l2dy) pushed a commit to branch master
in repository macports-base.
</pre>
<p><a href="https://github.com/macports/macports-base/commit/ad148dc6f27abbcd2e18d1d845796c30413d3a57">https://github.com/macports/macports-base/commit/ad148dc6f27abbcd2e18d1d845796c30413d3a57</a></p>
<pre style="white-space: pre; background: #F8F8F8">The following commit(s) were added to refs/heads/master by this push:
<span style='display:block; white-space:pre;color:#404040;'> new ad148dc6 darwintracelib1.0: merge duplicate code
</span>ad148dc6 is described below
<span style='display:block; white-space:pre;color:#808000;'>commit ad148dc6f27abbcd2e18d1d845796c30413d3a57
</span>Author: Zero King <l2dy@macports.org>
AuthorDate: Thu Feb 17 13:36:17 2022 +0000
<span style='display:block; white-space:pre;color:#404040;'> darwintracelib1.0: merge duplicate code
</span><span style='display:block; white-space:pre;color:#404040;'>
</span><span style='display:block; white-space:pre;color:#404040;'> Extract duplicate code into a function.
</span>---
src/darwintracelib1.0/darwintrace.c | 143 ++++++++++++++++--------------------
1 file changed, 62 insertions(+), 81 deletions(-)
<span style='display:block; white-space:pre;color:#808080;'>diff --git a/src/darwintracelib1.0/darwintrace.c b/src/darwintracelib1.0/darwintrace.c
</span><span style='display:block; white-space:pre;color:#808080;'>index 73509e98..cbc92b3b 100644
</span><span style='display:block; white-space:pre;background:#e0e0ff;'>--- a/src/darwintracelib1.0/darwintrace.c
</span><span style='display:block; white-space:pre;background:#e0e0ff;'>+++ b/src/darwintracelib1.0/darwintrace.c
</span><span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -713,6 +713,66 @@ static inline bool __darwintrace_sandbox_check(const char *path, int flags) {
</span> return false;
}
<span style='display:block; white-space:pre;background:#e0ffe0;'>+/* Private struct for __darwintrace_is_in_sandbox */
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+typedef struct {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *start;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ size_t len;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+} path_component_t;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+/*
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ * Helper function for __darwintrace_is_in_sandbox.
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ *
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ * \param[in] token path to parse
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ * \param[in] dst write position in normPath buffer
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ * \param[in] numComponents number of parsed components
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ * \param[in,out] pathComponents array of parsed components
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ * \param[out] normPath output buffer
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ * \return next numComponents
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ */
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+static size_t __parse_path_normalize(const char *token, char *dst, size_t numComponents, path_component_t *pathComponents, char *normPath) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ size_t idx;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ while ((idx = strcspn(token, "/")) > 0) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ // found a token, process it
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (token[0] == '\0' || token[0] == '/') {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ // empty entry, ignore
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ } else if (token[0] == '.' && (token[1] == '\0' || token[1] == '/')) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ // reference to current directory, ignore
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ } else if (token[0] == '.' && token[1] == '.' && (token[2] == '\0' || token[2] == '/')) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ // walk up one directory, but not if it's the last one, because /.. -> /
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (numComponents > 0) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ numComponents--;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (numComponents > 0) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ // move dst back to the previous entry
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ path_component_t *lastComponent = pathComponents + (numComponents - 1);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ dst = lastComponent->start + lastComponent->len + 1;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ } else {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ // we're at the top, move dst back to the beginning
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ dst = normPath + 1;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ } else {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ // copy token to normPath buffer (and null-terminate it)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ strlcpy(dst, token, idx + 1);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ dst[idx] = '\0';
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ // add descriptor entry for new token
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ pathComponents[numComponents].start = dst;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ pathComponents[numComponents].len = idx;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ numComponents++;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ // advance destination
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ dst += idx + 1;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (token[idx] == '\0') {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ break;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ token += idx + 1;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return numComponents;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> /**
* Check a path against the current sandbox
*
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -735,11 +795,6 @@ bool __darwintrace_is_in_sandbox(const char *path, int flags) {
</span> return true;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- typedef struct {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char *start;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- size_t len;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } path_component_t;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span> char normPath[MAXPATHLEN];
normPath[0] = '/';
normPath[1] = '\0';
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -831,44 +886,7 @@ bool __darwintrace_is_in_sandbox(const char *path, int flags) {
</span> /* Make sure the path is normalized. NOTE: Do _not_ use realpath(3) here.
* Doing so _will_ lead to problems. This is essentially a very simple
* re-implementation of realpath(3). */
<span style='display:block; white-space:pre;background:#ffe0e0;'>- while ((idx = strcspn(token, "/")) > 0) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // found a token, process it
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (token[0] == '\0' || token[0] == '/') {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // empty entry, ignore
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else if (token[0] == '.' && (token[1] == '\0' || token[1] == '/')) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // reference to current directory, ignore
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else if (token[0] == '.' && token[1] == '.' && (token[2] == '\0' || token[2] == '/')) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // walk up one directory, but not if it's the last one, because /.. -> /
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (numComponents > 0) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- numComponents--;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (numComponents > 0) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // move dst back to the previous entry
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- path_component_t *lastComponent = pathComponents + (numComponents - 1);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- dst = lastComponent->start + lastComponent->len + 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // we're at the top, move dst back to the beginning
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- dst = normPath + 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // copy token to normPath buffer (and null-terminate it)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- strlcpy(dst, token, idx + 1);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- dst[idx] = '\0';
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // add descriptor entry for new token
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- pathComponents[numComponents].start = dst;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- pathComponents[numComponents].len = idx;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- numComponents++;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // advance destination
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- dst += idx + 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (token[idx] == '\0') {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- break;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- token += idx + 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ numComponents = __parse_path_normalize(token, dst, numComponents, pathComponents, normPath);
</span>
// strip off resource forks
if (numComponents >= 2 &&
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -997,44 +1015,7 @@ bool __darwintrace_is_in_sandbox(const char *path, int flags) {
</span> }
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- while ((idx = strcspn(token, "/")) > 0) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // found a token, process it
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (token[0] == '\0' || token[0] == '/') {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // empty entry, ignore
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else if (token[0] == '.' && (token[1] == '\0' || token[1] == '/')) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // reference to current directory, ignore
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else if (token[0] == '.' && token[1] == '.' && (token[2] == '\0' || token[2] == '/')) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // walk up one directory, but not if it's the last one, because /.. -> /
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (numComponents > 0) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- numComponents--;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (numComponents > 0) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // move dst back to the previous entry
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- path_component_t *lastComponent = pathComponents + (numComponents - 1);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- dst = lastComponent->start + lastComponent->len + 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // we're at the top, move dst back to the beginning
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- dst = normPath + 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // copy token to normPath buffer
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- strlcpy(dst, token, idx + 1);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- dst[idx] = '\0';
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // add descriptor entry for new token
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- pathComponents[numComponents].start = dst;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- pathComponents[numComponents].len = idx;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- numComponents++;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // advance destination
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- dst += idx + 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (token[idx] == '\0') {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- break;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- token += idx + 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ numComponents = __parse_path_normalize(token, dst, numComponents, pathComponents, normPath);
</span> }
} while (pathIsSymlink);
</pre><pre style='margin:0'>
</pre>