hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/tools/perf/util/llvm-utils.c
....@@ -525,14 +525,37 @@
525525
526526 pr_debug("llvm compiling command template: %s\n", template);
527527
528
+ /*
529
+ * Below, substitute control characters for values that can cause the
530
+ * echo to misbehave, then substitute the values back.
531
+ */
528532 err = -ENOMEM;
529
- if (asprintf(&command_echo, "echo -n \"%s\"", template) < 0)
533
+ if (asprintf(&command_echo, "echo -n \a%s\a", template) < 0)
530534 goto errout;
531535
536
+#define SWAP_CHAR(a, b) do { if (*p == a) *p = b; } while (0)
537
+ for (char *p = command_echo; *p; p++) {
538
+ SWAP_CHAR('<', '\001');
539
+ SWAP_CHAR('>', '\002');
540
+ SWAP_CHAR('"', '\003');
541
+ SWAP_CHAR('\'', '\004');
542
+ SWAP_CHAR('|', '\005');
543
+ SWAP_CHAR('&', '\006');
544
+ SWAP_CHAR('\a', '"');
545
+ }
532546 err = read_from_pipe(command_echo, (void **) &command_out, NULL);
533547 if (err)
534548 goto errout;
535549
550
+ for (char *p = command_out; *p; p++) {
551
+ SWAP_CHAR('\001', '<');
552
+ SWAP_CHAR('\002', '>');
553
+ SWAP_CHAR('\003', '"');
554
+ SWAP_CHAR('\004', '\'');
555
+ SWAP_CHAR('\005', '|');
556
+ SWAP_CHAR('\006', '&');
557
+ }
558
+#undef SWAP_CHAR
536559 pr_debug("llvm compiling command : %s\n", command_out);
537560
538561 err = read_from_pipe(template, &obj_buf, &obj_buf_sz);