]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: sanitize variable names on export output
authorKarel Zak <kzak@redhat.com>
Thu, 3 Dec 2020 11:14:10 +0000 (12:14 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 3 Dec 2020 11:14:10 +0000 (12:14 +0100)
The shells are very restrictive about variable names, only [:alnum:]
chars are allowed (and alphabetic chars as the first char).  The
library will replace "bad" chars with "_". The char '%' at the end is
replaced by _PCT.

Addresses: https://github.com/karelzak/util-linux/issues/1201
Signed-off-by: Karel Zak <kzak@redhat.com>
include/carefulputc.h
libsmartcols/src/print.c
libsmartcols/src/table.c
misc-utils/findmnt.8
misc-utils/lsblk.8
sys-utils/lsipc.1
sys-utils/lsmem.1

index f1c03566da244cd0ac9b5658d0168dfee64fa704..81f1ad4631374fb1d04dc56b7f490233b6d01749 100644 (file)
@@ -151,5 +151,21 @@ static inline void fputs_nonblank(const char *data, FILE *out)
        }
 }
 
+static inline void fputs_shell_ident(const char *data, FILE *out)
+{
+       const char *p = data;
+
+       /* convert "1FOO" to "_1FOO" */
+       if (p && !isalpha(*p))
+               fputc('_', out);
+
+       /* replace all "bad" chars with "_" */
+       for (p = data; p && *p; p++) {
+               if (!isalnum(*p))
+                       fputc('_', out);
+               else
+                       fputc(*p, out);
+       }
+}
 
 #endif  /*  _CAREFULPUTC_H  */
index 9d78c90b3ef0c078a7262d51f3ae02324e6c71d0..2293a43faa92293cb78f9dea0f325c26c57fbdc9 100644 (file)
@@ -472,7 +472,10 @@ static int print_data(struct libscols_table *tb,
                return 0;
 
        case SCOLS_FMT_EXPORT:
-               fprintf(tb->out, "%s=", name);
+               fputs_shell_ident(name, tb->out);
+               if (endswith(name, "%"))
+                       fputs("PCT", tb->out);
+               fputc('=', tb->out);
                fputs_quoted(data, tb->out);
                if (!is_last)
                        fputs(colsep(tb), tb->out);
index a3ba21dc60d39d1fc20479debe361944b548d67b..d5c406bd7a38ad44e91b1344f0f4c2fabbf0f8b3 100644 (file)
@@ -1076,6 +1076,10 @@ int scols_table_enable_json(struct libscols_table *tb, int enable)
  * Enable/disable export output format (COLUMNAME="value" ...).
  * The parsable output formats (export and raw) are mutually exclusive.
  *
+ * Note that COLUMNAME maybe be modified on output to contains only chars
+ * allowed as shell variable identifiers, for example MIN-IO and FSUSE% will be
+ * MIN_IO and FSUSE_PCT.
+ *
  * Returns: 0 on success, negative number in case of an error.
  */
 int scols_table_enable_export(struct libscols_table *tb, int enable)
index e411c3010eae73105812e55d207222177ccdcd9b..86ab6ff9327e32e0b4b1fe27df0297b541ea32e2 100644 (file)
@@ -156,7 +156,9 @@ Output almost all available columns.  The columns that require
 are not included.
 .TP
 .BR \-P , " \-\-pairs"
-Use key="value" output format.  All potentially unsafe characters are hex-escaped (\\x<code>).
+Produce output in the form of key="value" pairs.  All potentially unsafe value characters are hex-escaped (\\x<code>).
+The key (variable name) will be modified to contain only characters allowed for a shell variable
+identifiers, for example, FS_OPTIONS and USE_PCT instead of FS-OPTIONS and USE%.
 .TP
 .BR \-p , " \-\-poll\fR[\fI=list\fR]"
 Monitor changes in the /proc/self/mountinfo file.  Supported actions are: mount,
index eeefd82829330e68444ea45d4e28b8f652606cec..f59e1d8b6a61ac3d70220ee112175be32f5336db 100644 (file)
@@ -128,7 +128,9 @@ Output all available columns.
 .TP
 .BR \-P , " \-\-pairs"
 Produce output in the form of key="value" pairs.  The output lines are still ordered by
-dependencies.  All potentially unsafe characters are hex-escaped (\\x<code>).
+dependencies.  All potentially unsafe value characters are hex-escaped (\\x<code>).
+The key (variable name) will be modified to contain only characters allowed for a shell variable
+identifiers, for example, MIN_IO and FSUSE_PCT instead of MIN-IO and FSUSE%.
 .TP
 .BR \-p , " \-\-paths"
 Print full device paths.
index b58b4cdf475671c86d6b4b14831d712eba7e053a..34ed5dae6a03e44e3515742646214019d7212162 100644 (file)
@@ -51,7 +51,10 @@ Write information about active semaphore sets.
 Show creator and owner.
 .TP
 \fB\-e\fR, \fB\-\-export\fR
-Output data in the format of NAME=VALUE.
+Produce output in the form of key="value" pairs.  All potentially unsafe value
+characters are hex-escaped (\\x<code>).  The key (variable name) will be
+modified to contain only characters allowed for a shell variable identifiers,
+for example, USE_PCT instead of USE%.
 .TP
 \fB\-J\fR, \fB\-\-json\fR
 Use the JSON output format.
index 2fe78b4c6fd4bc4b2217ae1aae27c3cf449a5dc3..e726b72cefd03a1ec8cf40cff141e37be3ecab7a 100644 (file)
@@ -61,8 +61,8 @@ specified in the format \fB+\fIlist\fP (e.g., \fBlsmem \-o +NODE\fP).
 Output all available columns.
 .TP
 .BR \-P , " \-\-pairs"
-Produce output in the form of key="value" pairs.
-All potentially unsafe characters are hex-escaped (\\x<code>).
+Produce output in the form of key="value" pairs.  All potentially unsafe value
+characters are hex-escaped (\\x<code>).
 .TP
 .BR \-r , " \-\-raw"
 Produce output in raw format.  All potentially unsafe characters are hex-escaped