mirror of
https://git.zx2c4.com/cgit
synced 2024-11-25 01:48:41 +00:00
Convert cgit_print_error to a variadic function
This removes many uses of "fmt" which uses a fixed size static pool of fixed size buffers. Instead of relying on these, we now pass around argument lists for as long as possible before using a strbuf to render content of an arbitrary size. Signed-off-by: John Keeping <john@keeping.me.uk>
This commit is contained in:
parent
d2e20e3814
commit
ed5bd30ebe
18
cgit.c
18
cgit.c
@ -459,7 +459,6 @@ static char *guess_defbranch(void)
|
|||||||
|
|
||||||
static int prepare_repo_cmd(struct cgit_context *ctx)
|
static int prepare_repo_cmd(struct cgit_context *ctx)
|
||||||
{
|
{
|
||||||
char *tmp;
|
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
int nongit = 0;
|
int nongit = 0;
|
||||||
int rc;
|
int rc;
|
||||||
@ -467,17 +466,16 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
|
|||||||
setenv("GIT_DIR", ctx->repo->path, 1);
|
setenv("GIT_DIR", ctx->repo->path, 1);
|
||||||
setup_git_directory_gently(&nongit);
|
setup_git_directory_gently(&nongit);
|
||||||
if (nongit) {
|
if (nongit) {
|
||||||
|
const char *name = ctx->repo->name;
|
||||||
rc = errno;
|
rc = errno;
|
||||||
ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
|
ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
|
||||||
"config error");
|
"config error");
|
||||||
tmp = fmt("Failed to open %s: %s",
|
|
||||||
ctx->repo->name,
|
|
||||||
rc ? strerror(rc) : "Not a valid git repository");
|
|
||||||
ctx->repo = NULL;
|
ctx->repo = NULL;
|
||||||
cgit_print_http_headers(ctx);
|
cgit_print_http_headers(ctx);
|
||||||
cgit_print_docstart(ctx);
|
cgit_print_docstart(ctx);
|
||||||
cgit_print_pageheader(ctx);
|
cgit_print_pageheader(ctx);
|
||||||
cgit_print_error(tmp);
|
cgit_print_error("Failed to open %s: %s", name,
|
||||||
|
rc ? strerror(rc) : "Not a valid git repository");
|
||||||
cgit_print_docend();
|
cgit_print_docend();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -501,14 +499,14 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (get_sha1(ctx->qry.head, sha1)) {
|
if (get_sha1(ctx->qry.head, sha1)) {
|
||||||
tmp = xstrdup(ctx->qry.head);
|
char *tmp = xstrdup(ctx->qry.head);
|
||||||
ctx->qry.head = ctx->repo->defbranch;
|
ctx->qry.head = ctx->repo->defbranch;
|
||||||
ctx->page.status = 404;
|
ctx->page.status = 404;
|
||||||
ctx->page.statusmsg = "Not found";
|
ctx->page.statusmsg = "Not found";
|
||||||
cgit_print_http_headers(ctx);
|
cgit_print_http_headers(ctx);
|
||||||
cgit_print_docstart(ctx);
|
cgit_print_docstart(ctx);
|
||||||
cgit_print_pageheader(ctx);
|
cgit_print_pageheader(ctx);
|
||||||
cgit_print_error(fmt("Invalid branch: %s", tmp));
|
cgit_print_error("Invalid branch: %s", tmp);
|
||||||
cgit_print_docend();
|
cgit_print_docend();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -550,7 +548,7 @@ static void process_request(void *cbdata)
|
|||||||
cgit_print_http_headers(ctx);
|
cgit_print_http_headers(ctx);
|
||||||
cgit_print_docstart(ctx);
|
cgit_print_docstart(ctx);
|
||||||
cgit_print_pageheader(ctx);
|
cgit_print_pageheader(ctx);
|
||||||
cgit_print_error(fmt("No repository selected"));
|
cgit_print_error("No repository selected");
|
||||||
cgit_print_docend();
|
cgit_print_docend();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -862,7 +860,7 @@ int main(int argc, const char **argv)
|
|||||||
err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
|
err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
|
||||||
ctx.qry.raw, ttl, process_request, &ctx);
|
ctx.qry.raw, ttl, process_request, &ctx);
|
||||||
if (err)
|
if (err)
|
||||||
cgit_print_error(fmt("Error processing page: %s (%d)",
|
cgit_print_error("Error processing page: %s (%d)",
|
||||||
strerror(err), err));
|
strerror(err), err);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
@ -94,12 +94,12 @@ void cgit_print_blob(const char *hex, char *path, const char *head)
|
|||||||
|
|
||||||
if (hex) {
|
if (hex) {
|
||||||
if (get_sha1_hex(hex, sha1)) {
|
if (get_sha1_hex(hex, sha1)) {
|
||||||
cgit_print_error(fmt("Bad hex value: %s", hex));
|
cgit_print_error("Bad hex value: %s", hex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (get_sha1(head, sha1)) {
|
if (get_sha1(head, sha1)) {
|
||||||
cgit_print_error(fmt("Bad ref: %s", head));
|
cgit_print_error("Bad ref: %s", head);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -113,13 +113,13 @@ void cgit_print_blob(const char *hex, char *path, const char *head)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (type == OBJ_BAD) {
|
if (type == OBJ_BAD) {
|
||||||
cgit_print_error(fmt("Bad object name: %s", hex));
|
cgit_print_error("Bad object name: %s", hex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = read_sha1_file(sha1, &type, &size);
|
buf = read_sha1_file(sha1, &type, &size);
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
cgit_print_error(fmt("Error reading object %s", hex));
|
cgit_print_error("Error reading object %s", hex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,12 +27,12 @@ void cgit_print_commit(char *hex, const char *prefix)
|
|||||||
hex = ctx.qry.head;
|
hex = ctx.qry.head;
|
||||||
|
|
||||||
if (get_sha1(hex, sha1)) {
|
if (get_sha1(hex, sha1)) {
|
||||||
cgit_print_error(fmt("Bad object id: %s", hex));
|
cgit_print_error("Bad object id: %s", hex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
commit = lookup_commit_reference(sha1);
|
commit = lookup_commit_reference(sha1);
|
||||||
if (!commit) {
|
if (!commit) {
|
||||||
cgit_print_error(fmt("Bad commit reference: %s", hex));
|
cgit_print_error("Bad commit reference: %s", hex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
info = cgit_parse_commit(commit);
|
info = cgit_parse_commit(commit);
|
||||||
|
@ -369,12 +369,12 @@ void cgit_print_diff(const char *new_rev, const char *old_rev,
|
|||||||
get_sha1(new_rev, new_rev_sha1);
|
get_sha1(new_rev, new_rev_sha1);
|
||||||
type = sha1_object_info(new_rev_sha1, &size);
|
type = sha1_object_info(new_rev_sha1, &size);
|
||||||
if (type == OBJ_BAD) {
|
if (type == OBJ_BAD) {
|
||||||
cgit_print_error(fmt("Bad object name: %s", new_rev));
|
cgit_print_error("Bad object name: %s", new_rev);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
commit = lookup_commit_reference(new_rev_sha1);
|
commit = lookup_commit_reference(new_rev_sha1);
|
||||||
if (!commit || parse_commit(commit)) {
|
if (!commit || parse_commit(commit)) {
|
||||||
cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
|
cgit_print_error("Bad commit: %s", sha1_to_hex(new_rev_sha1));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,12 +388,12 @@ void cgit_print_diff(const char *new_rev, const char *old_rev,
|
|||||||
if (!is_null_sha1(old_rev_sha1)) {
|
if (!is_null_sha1(old_rev_sha1)) {
|
||||||
type = sha1_object_info(old_rev_sha1, &size);
|
type = sha1_object_info(old_rev_sha1, &size);
|
||||||
if (type == OBJ_BAD) {
|
if (type == OBJ_BAD) {
|
||||||
cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
|
cgit_print_error("Bad object name: %s", sha1_to_hex(old_rev_sha1));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
commit2 = lookup_commit_reference(old_rev_sha1);
|
commit2 = lookup_commit_reference(old_rev_sha1);
|
||||||
if (!commit2 || parse_commit(commit2)) {
|
if (!commit2 || parse_commit(commit2)) {
|
||||||
cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
|
cgit_print_error("Bad commit: %s", sha1_to_hex(old_rev_sha1));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,12 +94,12 @@ void cgit_print_patch(char *hex, const char *prefix)
|
|||||||
hex = ctx.qry.head;
|
hex = ctx.qry.head;
|
||||||
|
|
||||||
if (get_sha1(hex, sha1)) {
|
if (get_sha1(hex, sha1)) {
|
||||||
cgit_print_error(fmt("Bad object id: %s", hex));
|
cgit_print_error("Bad object id: %s", hex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
commit = lookup_commit_reference(sha1);
|
commit = lookup_commit_reference(sha1);
|
||||||
if (!commit) {
|
if (!commit) {
|
||||||
cgit_print_error(fmt("Bad commit reference: %s", hex));
|
cgit_print_error("Bad commit reference: %s", hex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
info = cgit_parse_commit(commit);
|
info = cgit_parse_commit(commit);
|
||||||
|
15
ui-shared.c
15
ui-shared.c
@ -28,10 +28,21 @@ static char *http_date(time_t t)
|
|||||||
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||||
}
|
}
|
||||||
|
|
||||||
void cgit_print_error(const char *msg)
|
void cgit_print_error(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
cgit_vprint_error(fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cgit_vprint_error(const char *fmt, va_list ap)
|
||||||
|
{
|
||||||
|
va_list cp;
|
||||||
html("<div class='error'>");
|
html("<div class='error'>");
|
||||||
html_txt(msg);
|
va_copy(cp, ap);
|
||||||
|
html_vtxtf(fmt, cp);
|
||||||
|
va_end(cp);
|
||||||
html("</div>\n");
|
html("</div>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +52,10 @@ extern void cgit_object_link(struct object *obj);
|
|||||||
extern void cgit_submodule_link(const char *class, char *path,
|
extern void cgit_submodule_link(const char *class, char *path,
|
||||||
const char *rev);
|
const char *rev);
|
||||||
|
|
||||||
extern void cgit_print_error(const char *msg);
|
__attribute__((format (printf,1,2)))
|
||||||
|
extern void cgit_print_error(const char *fmt, ...);
|
||||||
|
__attribute__((format (printf,1,0)))
|
||||||
|
extern void cgit_vprint_error(const char *fmt, va_list ap);
|
||||||
extern void cgit_print_date(time_t secs, const char *format, int local_time);
|
extern void cgit_print_date(time_t secs, const char *format, int local_time);
|
||||||
extern void cgit_print_age(time_t t, time_t max_relative, const char *format);
|
extern void cgit_print_age(time_t t, time_t max_relative, const char *format);
|
||||||
extern void cgit_print_http_headers(struct cgit_context *ctx);
|
extern void cgit_print_http_headers(struct cgit_context *ctx);
|
||||||
|
@ -100,11 +100,11 @@ static int make_snapshot(const struct cgit_snapshot_format *format,
|
|||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
|
|
||||||
if (get_sha1(hex, sha1)) {
|
if (get_sha1(hex, sha1)) {
|
||||||
cgit_print_error(fmt("Bad object id: %s", hex));
|
cgit_print_error("Bad object id: %s", hex);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (!lookup_commit_reference(sha1)) {
|
if (!lookup_commit_reference(sha1)) {
|
||||||
cgit_print_error(fmt("Not a commit reference: %s", hex));
|
cgit_print_error("Not a commit reference: %s", hex);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
ctx.page.mimetype = xstrdup(format->mimetype);
|
ctx.page.mimetype = xstrdup(format->mimetype);
|
||||||
@ -154,13 +154,18 @@ static const char *get_ref_from_filename(const char *url, const char *filename,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_error(char *msg)
|
__attribute__((format (printf, 1, 2)))
|
||||||
|
static void show_error(char *fmt, ...)
|
||||||
{
|
{
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
ctx.page.mimetype = "text/html";
|
ctx.page.mimetype = "text/html";
|
||||||
cgit_print_http_headers(&ctx);
|
cgit_print_http_headers(&ctx);
|
||||||
cgit_print_docstart(&ctx);
|
cgit_print_docstart(&ctx);
|
||||||
cgit_print_pageheader(&ctx);
|
cgit_print_pageheader(&ctx);
|
||||||
cgit_print_error(msg);
|
va_start(ap, fmt);
|
||||||
|
cgit_vprint_error(fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
cgit_print_docend();
|
cgit_print_docend();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,8 +182,7 @@ void cgit_print_snapshot(const char *head, const char *hex,
|
|||||||
|
|
||||||
f = get_format(filename);
|
f = get_format(filename);
|
||||||
if (!f) {
|
if (!f) {
|
||||||
show_error(xstrdup(fmt("Unsupported snapshot format: %s",
|
show_error("Unsupported snapshot format: %s", filename);
|
||||||
filename)));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,12 +374,11 @@ void cgit_show_stats(struct cgit_context *ctx)
|
|||||||
|
|
||||||
i = cgit_find_stats_period(code, &period);
|
i = cgit_find_stats_period(code, &period);
|
||||||
if (!i) {
|
if (!i) {
|
||||||
cgit_print_error(fmt("Unknown statistics type: %c", code[0]));
|
cgit_print_error("Unknown statistics type: %c", code[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (i > ctx->repo->max_stats) {
|
if (i > ctx->repo->max_stats) {
|
||||||
cgit_print_error(fmt("Statistics type disabled: %s",
|
cgit_print_error("Statistics type disabled: %s", period->name);
|
||||||
period->name));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
authors = collect_stats(ctx, period);
|
authors = collect_stats(ctx, period);
|
||||||
|
6
ui-tag.c
6
ui-tag.c
@ -50,18 +50,18 @@ void cgit_print_tag(char *revname)
|
|||||||
revname = ctx.qry.head;
|
revname = ctx.qry.head;
|
||||||
|
|
||||||
if (get_sha1(fmt("refs/tags/%s", revname), sha1)) {
|
if (get_sha1(fmt("refs/tags/%s", revname), sha1)) {
|
||||||
cgit_print_error(fmt("Bad tag reference: %s", revname));
|
cgit_print_error("Bad tag reference: %s", revname);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
obj = parse_object(sha1);
|
obj = parse_object(sha1);
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
cgit_print_error(fmt("Bad object id: %s", sha1_to_hex(sha1)));
|
cgit_print_error("Bad object id: %s", sha1_to_hex(sha1));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (obj->type == OBJ_TAG) {
|
if (obj->type == OBJ_TAG) {
|
||||||
tag = lookup_tag(sha1);
|
tag = lookup_tag(sha1);
|
||||||
if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) {
|
if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) {
|
||||||
cgit_print_error(fmt("Bad tag object: %s", revname));
|
cgit_print_error("Bad tag object: %s", revname);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
html("<table class='commit-info'>\n");
|
html("<table class='commit-info'>\n");
|
||||||
|
13
ui-tree.c
13
ui-tree.c
@ -95,15 +95,13 @@ static void print_object(const unsigned char *sha1, char *path, const char *base
|
|||||||
|
|
||||||
type = sha1_object_info(sha1, &size);
|
type = sha1_object_info(sha1, &size);
|
||||||
if (type == OBJ_BAD) {
|
if (type == OBJ_BAD) {
|
||||||
cgit_print_error(fmt("Bad object name: %s",
|
cgit_print_error("Bad object name: %s", sha1_to_hex(sha1));
|
||||||
sha1_to_hex(sha1)));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = read_sha1_file(sha1, &type, &size);
|
buf = read_sha1_file(sha1, &type, &size);
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
cgit_print_error(fmt("Error reading object %s",
|
cgit_print_error("Error reading object %s", sha1_to_hex(sha1));
|
||||||
sha1_to_hex(sha1)));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,8 +207,7 @@ static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_cont
|
|||||||
|
|
||||||
tree = parse_tree_indirect(sha1);
|
tree = parse_tree_indirect(sha1);
|
||||||
if (!tree) {
|
if (!tree) {
|
||||||
cgit_print_error(fmt("Not a tree object: %s",
|
cgit_print_error("Not a tree object: %s", sha1_to_hex(sha1));
|
||||||
sha1_to_hex(sha1)));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,12 +270,12 @@ void cgit_print_tree(const char *rev, char *path)
|
|||||||
rev = ctx.qry.head;
|
rev = ctx.qry.head;
|
||||||
|
|
||||||
if (get_sha1(rev, sha1)) {
|
if (get_sha1(rev, sha1)) {
|
||||||
cgit_print_error(fmt("Invalid revision name: %s", rev));
|
cgit_print_error("Invalid revision name: %s", rev);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
commit = lookup_commit_reference(sha1);
|
commit = lookup_commit_reference(sha1);
|
||||||
if (!commit || parse_commit(commit)) {
|
if (!commit || parse_commit(commit)) {
|
||||||
cgit_print_error(fmt("Invalid commit reference: %s", rev));
|
cgit_print_error("Invalid commit reference: %s", rev);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user