aboutsummaryrefslogtreecommitdiffstats
path: root/ui-blob.c
diff options
context:
space:
mode:
authorLukas Fleischer2013-03-03 17:27:46 +0100
committerJason A. Donenfeld2013-03-04 09:12:54 -0500
commit41f9c4e2f66252c83c6524fa4a346839d4c454b2 (patch)
tree2a80d6032308597cfebcf194f8860bf708bc13f4 /ui-blob.c
parent973deda0eaf4e16c8cc0b25ca1bff1faae17584e (diff)
downloadcgit-41f9c4e2f66252c83c6524fa4a346839d4c454b2.tar.gz
cgit-41f9c4e2f66252c83c6524fa4a346839d4c454b2.tar.bz2
cgit-41f9c4e2f66252c83c6524fa4a346839d4c454b2.zip
ui-blob.c: Use a context structure in walk_tree()
Do not misuse global variables to save the context. Instead, use the context pointer which was designed to share information between a read_tree_fn and the caller. This also prevents from potential misuse of the global pointers match_path and matched_sha1 after the referenced values have been overwritten on the stack. Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de>
Diffstat (limited to 'ui-blob.c')
-rw-r--r--ui-blob.c42
1 files changed, 26 insertions, 16 deletions
diff --git a/ui-blob.c b/ui-blob.c
index d382ba3..c59fbcb 100644
--- a/ui-blob.c
+++ b/ui-blob.c
@@ -11,17 +11,22 @@
11#include "html.h" 11#include "html.h"
12#include "ui-shared.h" 12#include "ui-shared.h"
13 13
14static char *match_path; 14struct walk_tree_context {
15static unsigned char *matched_sha1; 15 char *match_path;
16static int found_path; 16 unsigned char *matched_sha1;
17 int found_path;
18};
17 19
18static int walk_tree(const unsigned char *sha1, const char *base, int baselen, 20static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
19 const char *pathname, unsigned mode, int stage, void *cbdata) { 21 const char *pathname, unsigned mode, int stage, void *cbdata)
20 if (strncmp(base, match_path, baselen) 22{
21 || strcmp(match_path + baselen, pathname)) 23 struct walk_tree_context *walk_tree_ctx = cbdata;
24
25 if (strncmp(base, walk_tree_ctx->match_path, baselen)
26 || strcmp(walk_tree_ctx->match_path + baselen, pathname))
22 return READ_TREE_RECURSIVE; 27 return READ_TREE_RECURSIVE;
23 memmove(matched_sha1, sha1, 20); 28 memmove(walk_tree_ctx->matched_sha1, sha1, 20);
24 found_path = 1; 29 walk_tree_ctx->found_path = 1;
25 return 0; 30 return 0;
26} 31}
27 32
@@ -40,16 +45,19 @@ int cgit_print_file(char *path, const char *head)
40 .nr = 1, 45 .nr = 1,
41 .items = &path_items 46 .items = &path_items
42 }; 47 };
48 struct walk_tree_context walk_tree_ctx = {
49 .match_path = path,
50 .matched_sha1 = sha1,
51 .found_path = 0
52 };
53
43 if (get_sha1(head, sha1)) 54 if (get_sha1(head, sha1))
44 return -1; 55 return -1;
45 type = sha1_object_info(sha1, &size); 56 type = sha1_object_info(sha1, &size);
46 if (type == OBJ_COMMIT && path) { 57 if (type == OBJ_COMMIT && path) {
47 commit = lookup_commit_reference(sha1); 58 commit = lookup_commit_reference(sha1);
48 match_path = path; 59 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
49 matched_sha1 = sha1; 60 if (!walk_tree_ctx.found_path)
50 found_path = 0;
51 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, NULL);
52 if (!found_path)
53 return -1; 61 return -1;
54 type = sha1_object_info(sha1, &size); 62 type = sha1_object_info(sha1, &size);
55 } 63 }
@@ -78,6 +86,10 @@ void cgit_print_blob(const char *hex, char *path, const char *head)
78 .nr = 1, 86 .nr = 1,
79 .items = &path_items 87 .items = &path_items
80 }; 88 };
89 struct walk_tree_context walk_tree_ctx = {
90 .match_path = path,
91 .matched_sha1 = sha1,
92 };
81 93
82 if (hex) { 94 if (hex) {
83 if (get_sha1_hex(hex, sha1)) { 95 if (get_sha1_hex(hex, sha1)) {
@@ -95,9 +107,7 @@ void cgit_print_blob(const char *hex, char *path, const char *head)
95 107
96 if ((!hex) && type == OBJ_COMMIT && path) { 108 if ((!hex) && type == OBJ_COMMIT && path) {
97 commit = lookup_commit_reference(sha1); 109 commit = lookup_commit_reference(sha1);
98 match_path = path; 110 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
99 matched_sha1 = sha1;
100 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, NULL);
101 type = sha1_object_info(sha1,&size); 111 type = sha1_object_info(sha1,&size);
102 } 112 }
103 113