summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Kent Hajnal <software@alephnull.net>2024-05-17 16:44:55 -0400
committerAlex Kent Hajnal <software@alephnull.net>2024-05-17 16:44:55 -0400
commitd022bed22efde27509ebaf5a44a53c2ce3fda2de (patch)
treef7b86d0a96644417fa780273c9fac7bbb7fdb2bc
parent2efb6404b9ff1eaa88f0e47c543c826be2f2e5c0 (diff)
downloadtdelibs-feat/tdeio-xattr-support.tar.gz
tdelibs-feat/tdeio-xattr-support.zip
Additional sanity checks, Removed quote() and quote_free() functions since they can't cleanly handle errorsfeat/tdeio-xattr-support
Signed-off-by: Alex Kent Hajnal <software@alephnull.net>
-rw-r--r--tdeio/tdeio/job.cpp90
1 files 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 <stdio.h> // For stderr, vsnprintf (already included above)
+//#include <stdio.h> // For vsnprintf (already included above)
//#include <errno.h> // For errno (already included above)
-//#include <stdlib.h> // For free (already included above)
-//#include <string.h> // For strerror, strdup, and strncmp (already implicitly included above)
+//#include <stdlib.h> // For malloc and free (already included above)
+//#include <string.h> // For strerror and strncmp (already implicitly included above)
#include <stdarg.h> // 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):