Author: imp Date: Wed Jun 27 04:10:48 2018 New Revision: 335689 URL: https://svnweb.freebsd.org/changeset/base/335689
Log: Create new devctl_safe_quote_sb to copy a source string into a struct sbuf to make it safe. Callers are expected to add the " " around it, if needed. Sponsored by: Netflix Differential Review: https://reviews.freebsd.org/D16026 Modified: head/sys/kern/subr_bus.c head/sys/sys/bus.h Modified: head/sys/kern/subr_bus.c ============================================================================== --- head/sys/kern/subr_bus.c Wed Jun 27 03:58:02 2018 (r335688) +++ head/sys/kern/subr_bus.c Wed Jun 27 04:10:48 2018 (r335689) @@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include <machine/bus.h> #include <sys/random.h> #include <sys/rman.h> +#include <sys/sbuf.h> #include <sys/selinfo.h> #include <sys/signalvar.h> #include <sys/smp.h> @@ -880,6 +881,29 @@ devctl_safe_quote(char *dst, const char *src, size_t l *walker++ = *src++; } *walker = '\0'; +} + +/** + * @brief safely quotes strings that might have double quotes in them. + * + * The devctl protocol relies on quoted strings having matching quotes. + * This routine quotes any internal quotes so the resulting string + * is safe to pass to snprintf to construct, for example pnp info strings. + * Strings are always terminated with a NUL, but may be truncated if longer + * than @p len bytes after quotes. + * + * @param sb sbuf to place the characters into + * @param src Original buffer. + */ +void +devctl_safe_quote_sb(struct sbuf *sb, const char *src) +{ + + while (*src != '\0') { + if (*src == '"' || *src == '\\') + sbuf_putc(sb, '\\'); + sbuf_putc(sb, *src++); + } } /* End of /dev/devctl code */ Modified: head/sys/sys/bus.h ============================================================================== --- head/sys/sys/bus.h Wed Jun 27 03:58:02 2018 (r335688) +++ head/sys/sys/bus.h Wed Jun 27 04:10:48 2018 (r335689) @@ -156,7 +156,9 @@ void devctl_notify(const char *__system, const char *_ const char *__type, const char *__data); void devctl_queue_data_f(char *__data, int __flags); void devctl_queue_data(char *__data); -void devctl_safe_quote(char *__dst, const char *__src, size_t len); +void devctl_safe_quote(char *__dst, const char *__src, size_t __len); +struct sbuf; +void devctl_safe_quote_sb(struct sbuf *__sb, const char *__src); /** * Device name parsers. Hook to allow device enumerators to map _______________________________________________ svn-src-head@freebsd.org mailing list https://lists.freebsd.org/mailman/listinfo/svn-src-head To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"