svn commit: r353009 - head/lib/libusb

Kyle Evans kevans at FreeBSD.org
Wed Oct 2 15:19:40 UTC 2019


Author: kevans
Date: Wed Oct  2 15:19:39 2019
New Revision: 353009
URL: https://svnweb.freebsd.org/changeset/base/353009

Log:
  libusb: LIBUSB_DEBUG environment variable override of libusb_set_debug
  
  The debug level generally just controls verbosity of libusb for debugging
  libusb devices/usage. We allow the environment to set the debug level
  independent of the application, but the application will always override
  this if it explicitly sets the debug level.
  
  Changing the environment is easy, but patching the software to change the
  debug level isn't necessarily easy or possible. Further, there's this
  write-only debug_fixed variable that would seem to imply that the debug
  level should be fixed, but it isn't currently used. Change the logic to use
  strtol() so we can detect real 0 vs. conversion failure, then honor
  debug_fixed in libusb_set_debug.
  
  Reviewed by:	hselasky
  MFC after:	3 days
  Differential Revision:	https://reviews.freebsd.org/D21877

Modified:
  head/lib/libusb/libusb10.c

Modified: head/lib/libusb/libusb10.c
==============================================================================
--- head/lib/libusb/libusb10.c	Wed Oct  2 15:13:40 2019	(r353008)
+++ head/lib/libusb/libusb10.c	Wed Oct  2 15:19:39 2019	(r353009)
@@ -91,7 +91,8 @@ void
 libusb_set_debug(libusb_context *ctx, int level)
 {
 	ctx = GET_CONTEXT(ctx);
-	if (ctx)
+	/* debug_fixed is set when the environment overrides libusb_set_debug */
+	if (ctx && ctx->debug_fixed == 0)
 		ctx->debug = level;
 }
 
@@ -132,7 +133,7 @@ libusb_init(libusb_context **context)
 {
 	struct libusb_context *ctx;
 	pthread_condattr_t attr;
-	char *debug;
+	char *debug, *ep;
 	int ret;
 
 	ctx = malloc(sizeof(*ctx));
@@ -143,9 +144,23 @@ libusb_init(libusb_context **context)
 
 	debug = getenv("LIBUSB_DEBUG");
 	if (debug != NULL) {
-		ctx->debug = atoi(debug);
-		if (ctx->debug != 0)
+		/*
+		 * If LIBUSB_DEBUG is set, we'll honor that and use it to
+		 * override libusb_set_debug calls.
+		 */
+		errno = 0;
+		ctx->debug = strtol(debug, &ep, 10);
+		if (errno == 0 && *ep == '\0') {
 			ctx->debug_fixed = 1;
+		} else {
+			/*
+			 * LIBUSB_DEBUG conversion failed for some reason, but
+			 * we don't care about the specifics all that much.  We
+			 * can't use it either way.  Force it to the default,
+			 * 0, in case we had a partial number.
+			 */
+			ctx->debug = 0;
+		}
 	}
 	TAILQ_INIT(&ctx->pollfds);
 	TAILQ_INIT(&ctx->tr_done);


More information about the svn-src-all mailing list