]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Improve how NotePairs::find() reports its results (#2175)
authorJoshua Rogers <MegaManSec@users.noreply.github.com>
Mon, 8 Sep 2025 17:54:42 +0000 (17:54 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Mon, 8 Sep 2025 17:54:53 +0000 (17:54 +0000)
Replaced a problematic combination of a boolean return type and an SBuf
out-parameter with a safer and more readable optional SBuf return type.

src/Notes.cc
src/Notes.h
src/auth/UserRequest.cc
src/auth/digest/UserRequest.cc
src/format/Format.cc

index 13a3368629f7987918928044d09c4f034c99fe3d..618b7fb2ddb21f5b889c702a9aa2e375eb3525d1 100644 (file)
@@ -277,10 +277,11 @@ Notes::printAsAnnotationAclParameters(std::ostream &os) const
     }
 }
 
-bool
-NotePairs::find(SBuf &resultNote, const char *noteKey, const char *sep) const
+std::optional<SBuf>
+NotePairs::find(const char *noteKey, const char *sep) const
 {
-    resultNote.clear();
+    SBuf resultNote;
+
     for (const auto &e: entries) {
         if (!e->name().cmp(noteKey)) {
             if (!resultNote.isEmpty())
@@ -288,7 +289,11 @@ NotePairs::find(SBuf &resultNote, const char *noteKey, const char *sep) const
             resultNote.append(e->value());
         }
     }
-    return resultNote.length();
+
+    if (resultNote.isEmpty())
+        return std::nullopt;
+
+    return resultNote;
 }
 
 void
index f25c56bc36d35accb6e4e2ee216ade12b971b90a..2fcdf6ded0c315d4acec331180b03789b38d64a5 100644 (file)
@@ -17,6 +17,7 @@
 #include "SquidString.h"
 
 #include <iosfwd>
+#include <optional>
 #include <string>
 #include <vector>
 
@@ -225,10 +226,9 @@ public:
     /// Entries which already exist in the destination set are ignored.
     void appendNewOnly(const NotePairs *src);
 
-    /// \param resultNote a comma separated list of notes with key 'noteKey'.
-    /// \returns true if there are entries with the given 'noteKey'.
+    /// \returns a comma separated list of notes with key 'noteKey', if any.
     /// Use findFirst() instead when a unique kv-pair is needed.
-    bool find(SBuf &resultNote, const char *noteKey, const char *sep = ",") const;
+    std::optional<SBuf> find(const char *noteKey, const char *sep = ",") const;
 
     /// \returns the first note value for this key or an empty string.
     const char *findFirst(const char *noteKey) const;
index 3a20a67b5f89c8127256d651aa0faaa2515dbb2e..6a640ea71051766c1445db525ae49d090053e642 100644 (file)
@@ -25,6 +25,7 @@
 #include "HttpReply.h"
 #include "HttpRequest.h"
 #include "MemBuf.h"
+#include "sbuf/Stream.h"
 
 /* Generic Functions */
 
@@ -575,11 +576,11 @@ Auth::UserRequest::helperRequestKeyExtras(HttpRequest *request, AccessLogEntry::
 void
 Auth::UserRequest::denyMessageFromHelper(const char *proto, const Helper::Reply &reply)
 {
-    static SBuf messageNote;
-    if (!reply.notes.find(messageNote, "message")) {
-        messageNote.append(proto);
-        messageNote.append(" Authentication denied with no reason given");
+    auto messageNote = reply.notes.find("message");
+
+    if (!messageNote) {
+        messageNote = ToSBuf(proto, " Authentication denied with no reason given");
     }
-    setDenyMessage(messageNote.c_str());
-}
 
+    setDenyMessage(messageNote->c_str());
+}
index b3c468ea9d50d879b568f080d38c1f0f333fa2bb..2ad3d24d1bf07ed14c3bf46edc5fba0de4f515ac 100644 (file)
@@ -362,9 +362,8 @@ Auth::Digest::UserRequest::HandleReply(void *data, const Helper::Reply &reply)
         digest_request->user()->credentials(Auth::Failed);
         digest_request->flags.invalid_password = true;
 
-        SBuf msgNote;
-        if (reply.notes.find(msgNote, "message")) {
-            digest_request->setDenyMessage(msgNote.c_str());
+        if (auto msgNote = reply.notes.find("message")) {
+            digest_request->setDenyMessage(msgNote->c_str());
         } else if (reply.other().hasContent()) {
             // old helpers did send ERR result but a bare message string instead of message= key name.
             // TODO deprecate and remove old auth digest helper protocol
index 711082379c6c3653da8e1d612c833de97c8cdcfe..04efd6906929f68c5ad58dd7ff1ac9a74ff5abea 100644 (file)
@@ -1390,19 +1390,18 @@ Format::Format::assemble(MemBuf &mb, const AccessLogEntry::Pointer &al, int logS
             tmp[1] = '\0';
             if (fmt->data.header.header && *fmt->data.header.header) {
                 const char *separator = tmp;
-                static SBuf note;
 #if USE_ADAPTATION
                 Adaptation::History::Pointer ah = al->request ? al->request->adaptHistory() : Adaptation::History::Pointer();
                 if (ah && ah->metaHeaders) {
-                    if (ah->metaHeaders->find(note, fmt->data.header.header, separator))
-                        sb.append(note);
+                    if (const auto note = ah->metaHeaders->find(fmt->data.header.header, separator))
+                        sb.append(*note);
                 }
 #endif
                 if (al->notes) {
-                    if (al->notes->find(note, fmt->data.header.header, separator)) {
+                    if (const auto note = al->notes->find(fmt->data.header.header, separator)) {
                         if (!sb.isEmpty())
                             sb.append(separator);
-                        sb.append(note);
+                        sb.append(*note);
                     }
                 }
                 out = sb.c_str();