What changed, and why it matters
This commit adds a new helper function that converts text into URL-encoded format (for example, turning spaces into '+' and special characters into '%XX'). It also makes a tiny internal helper function non-static-inline. There is no indication this fixes a security bug; it appears to be a routine feature addition.
No security action required. Reviewers may optionally verify callers pass adequate dest_len and check the return value, but the function itself is defensive.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces urlencode() alongside the existing urldecode() in main/utils/urldecode.c/h. It uses isalnum/isxdigit, maps unreserved characters verbatim, encodes space as ‘+’, and other bytes as ‘%02X’ via snprintf. Bounds checks truncate and return false if dest_len is insufficient. The only change to existing code is removing ‘inline’ from map_char(). No security vulnerability is evident from the diff.
Changed components
main/utils/urldecode.cmain/utils/urldecode.hInspect captured patch +51 / −1
diff --git a/main/utils/urldecode.c b/main/utils/urldecode.c
index 8ca9878..7adb9c9 100644
--- a/main/utils/urldecode.c
+++ b/main/utils/urldecode.c
@@ -3,8 +3,9 @@
#include "../jade_assert.h"
#include <ctype.h>
+#include <stdio.h>
-static inline char map_char(char c)
+static char map_char(char c)
{
// Helper to map url-encoded %-escaped character
JADE_ASSERT(isxdigit(c));
@@ -65,4 +66,52 @@ bool urldecode(const char* src, const size_t src_len, char* dest, const size_t d
*dest = '\0';
return true;
}
+
+// Simple urlencode function - special chars are replaced by %XX, space is replaced by '+',
+// and anything else is copied verbatim.
+// The output string is always nul-terminated (although the input need not be).
+bool urlencode(const char* src, const size_t src_len, char* dest, const size_t dest_len)
+{
+ JADE_ASSERT(src);
+ JADE_ASSERT(src_len);
+ JADE_ASSERT(dest);
+ JADE_ASSERT(dest_len);
+
+ const char* src_end = src + src_len;
+ const char* dest_end = dest + dest_len;
+
+ while (src < src_end) {
+ if (dest > dest_end - 2) {
+ // Destination insufficient - need at least 1 char for encoding and 1 for nul-terminator.
+ // Truncate (terminate) here and return false.
+ *dest = '\0';
+ return false;
+ }
+
+ if (isalnum((unsigned char)*src) || *src == '-' || *src == '_' || *src == '.' || *src == '~') {
+ // Non-encoded character - copy across
+ *dest++ = *src++;
+ } else if (*src == ' ') {
+ // Space is encoded as '+'
+ *dest++ = '+';
+ ++src;
+ } else {
+ if (dest > dest_end - 4) {
+ // Destination insufficient - need 3 chars for encoding and 1 for nul-terminator.
+ // Truncate (terminate) here and return false.
+ *dest = '\0';
+ return false;
+ }
+
+ // Encode as %XX
+ snprintf(dest, dest_end - dest, "%%%02X", (unsigned char)*src);
+ ++src;
+ dest += 3;
+ }
+ }
+
+ JADE_ASSERT(dest < dest_end);
+ *dest = '\0';
+ return true;
+}
#endif // AMALGAMATED_BUILD
diff --git a/main/utils/urldecode.h b/main/utils/urldecode.h
index 98c9cd6..e2460a9 100644
--- a/main/utils/urldecode.h
+++ b/main/utils/urldecode.h
@@ -5,5 +5,6 @@
#include <stddef.h>
bool urldecode(const char* src, size_t src_len, char* dest, size_t dest_len);
+bool urlencode(const char* src, size_t src_len, char* dest, size_t dest_len);
#endif /* UTILS_URLDECODE_H_ */
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.