summaryrefslogtreecommitdiffstats
path: root/src/diag.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-06-16 13:44:33 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-06-16 13:44:33 -0400
commit6a7211d673473c542aad676bec8089c37b71476d (patch)
tree5a59bb533e8d0267dd156b51d1edadd692a0344b /src/diag.c
parent2eeee3e0d0d3aeca5e6487cc2431c1f65e35a6d8 (diff)
downloadbfs-6a7211d673473c542aad676bec8089c37b71476d.tar.xz
diag: Shell-escape arguments for argv warnings/errors
Diffstat (limited to 'src/diag.c')
-rw-r--r--src/diag.c23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/diag.c b/src/diag.c
index 04e3678..9e6b7ae 100644
--- a/src/diag.c
+++ b/src/diag.c
@@ -5,10 +5,12 @@
#include "bfstd.h"
#include "ctx.h"
#include "color.h"
+#include "dstring.h"
#include "expr.h"
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
+#include <string.h>
noreturn void bfs_abortf(const char *format, ...) {
va_list args;
@@ -149,6 +151,17 @@ static void bfs_argv_diag(const struct bfs_ctx *ctx, const bool *args, bool warn
bfs_error_prefix(ctx);
}
+ char *argv[ctx->argc];
+ for (size_t i = 0; i < ctx->argc; ++i) {
+ argv[i] = wordesc(ctx->argv[i]);
+ if (!argv[i]) {
+ for (size_t j = 0; j < i; ++j) {
+ free(argv[j]);
+ }
+ return;
+ }
+ }
+
size_t max_argc = 0;
for (size_t i = 0; i < ctx->argc; ++i) {
if (i > 0) {
@@ -157,9 +170,9 @@ static void bfs_argv_diag(const struct bfs_ctx *ctx, const bool *args, bool warn
if (args[i]) {
max_argc = i + 1;
- cfprintf(ctx->cerr, "${bld}%s${rs}", ctx->argv[i]);
+ cfprintf(ctx->cerr, "${bld}%s${rs}", argv[i]);
} else {
- cfprintf(ctx->cerr, "%s", ctx->argv[i]);
+ cfprintf(ctx->cerr, "%s", argv[i]);
}
}
@@ -188,7 +201,7 @@ static void bfs_argv_diag(const struct bfs_ctx *ctx, const bool *args, bool warn
}
}
- size_t len = xstrwidth(ctx->argv[i]);
+ size_t len = xstrwidth(argv[i]);
for (size_t j = 0; j < len; ++j) {
if (args[i]) {
cfprintf(ctx->cerr, "~");
@@ -203,6 +216,10 @@ static void bfs_argv_diag(const struct bfs_ctx *ctx, const bool *args, bool warn
}
cfprintf(ctx->cerr, "\n");
+
+ for (size_t i = 0; i < ctx->argc; ++i) {
+ free(argv[i]);
+ }
}
void bfs_argv_error(const struct bfs_ctx *ctx, const bool *args) {