From d022bed22efde27509ebaf5a44a53c2ce3fda2de Mon Sep 17 00:00:00 2001 From: Alex Kent Hajnal Date: Fri, 17 May 2024 16:44:55 -0400 Subject: Additional sanity checks, Removed quote() and quote_free() functions since they can't cleanly handle errors Signed-off-by: Alex Kent Hajnal --- tdeio/tdeio/job.cpp | 90 ++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/tdeio/tdeio/job.cpp b/tdeio/tdeio/job.cpp index 726691d27..839a7bfd6 100644 --- a/tdeio/tdeio/job.cpp +++ b/tdeio/tdeio/job.cpp @@ -97,10 +97,10 @@ extern "C" { #ifdef HAVE_ATTR__ATTR_COPY_FILE -//#include // For stderr, vsnprintf (already included above) +//#include // For vsnprintf (already included above) //#include // For errno (already included above) -//#include // For free (already included above) -//#include // For strerror, strdup, and strncmp (already implicitly included above) +//#include // For malloc and free (already included above) +//#include // For strerror and strncmp (already implicitly included above) #include // For va_start and va_end extern "C" { @@ -131,61 +131,52 @@ error(struct error_context *ctx, const char *fmt, ...) va_list ap; - // Save a copy in case it gets clobbered (shouldn't be needed) + // Save a copy since it may get clobbered int our_errno = errno; - // Get length of formatted string (may be 0) - va_start(ap, fmt); - int len = vsnprintf(nullptr, 0, fmt, ap); - va_end(ap); - - if ( len ) { - // There's a prefix string, output it - char* buffer = (char*)malloc(len+1); - if ( buffer ) { - // Buffer allocated, write the error to it - va_start(ap, fmt); - vsnprintf(&buffer[0], len+1, fmt, ap); - va_end(ap); - // Write message to log - kdDebug(7007) << "Error in attr_copy_file: " << buffer << ": " << strerror(our_errno) << endl; + if ( fmt ) { + // Format string was supplied + + // Get length of formatted string (may be 0) + va_start(ap, fmt); + int len = vsnprintf(nullptr, 0, fmt, ap); + va_end(ap); + + if ( len > 0 ) { + // There's a prefix string, output it + char* buffer = (char*)malloc(len+1); + if ( buffer ) { + // Buffer allocated, write the error to it + va_start(ap, fmt); + int written = vsnprintf(&buffer[0], len+1, fmt, ap); + va_end(ap); + if ( written > 0 ) { + // Write message to log + kdDebug(7007) << "Error in attr_copy_file: " << buffer << ": " << strerror(our_errno) << endl; + } else { + // vsnprintf failed or returned an empty string, just log what we can + kdDebug(7007) << "Error in attr_copy_file: " << strerror(our_errno) << endl; + } + } else { + // Buffer allocation failed, just log what we can + kdDebug(7007) << "Error in attr_copy_file: " << strerror(our_errno) << endl; + } + free(buffer); + } else { - // Buffer allocation failed, just log what we can + // No prefix string, just log the error code kdDebug(7007) << "Error in attr_copy_file: " << strerror(our_errno) << endl; } - free(buffer); - } else { - // No prefix string, just log the error code + // No format string, just log the error code kdDebug(7007) << "Error in attr_copy_file: " << strerror(our_errno) << endl; } } -/* - * Optional handler for quoting path names in error messages. - * (This is a very stupid example!) - */ -static const char * -quote(struct error_context *ctx, const char *pathname) -{ - char *pn = strdup(pathname), *p; - pathname = strdup(pathname); - for (p = pn; *p != '\0'; p++) - if (*p & 0x80) - *p='?'; - return pn; -} - -static void -quote_free(struct error_context *ctx, const char *name) -{ - free((void *)name); -} - /* * The error context we pass to attr_copy_file(). */ -struct error_context ctx = { error, quote, quote_free }; +struct error_context ctx = { error, NULL, NULL }; /* * Optional attribute filter for attr_copy_file(). This example @@ -205,7 +196,14 @@ static int is_user_attr(const char *name, struct error_context *ctx) // Note that non-Linux systems (including Solaris and Darwin/OSX) may // use a different naming scheme for user attributes. AFAIK FreeBSD // and NetBSD do support the "user" namespace. - return strncmp(name, "user.", 5) == 0; + + if ( name ) { + // Name is not a null pointer + return strncmp(name, "user.", 5) == 0; + } else { + // Name is a null pointer + return -1; // Not matched + } // If this function is not specified in the call to attr_copy_file then the // default behavior is to use the following heuristic instead (see above): -- cgit v1.2.1