Skip to content

Commit b85900e

Browse files
committed
refactor(crypto::uuid): return pointers from UUID helpers; correct Ns_HexString attributes
* Change uuid_format() to return char* (dst) instead of void; this allows PURE attribute and provides call‑site chaining and clearer intent. * Similar for uuid_v4()/uuid_v7() * Dropped NS_GNUC_PURE in include/ns.h since it breaks code for some compilers.
1 parent 310925a commit b85900e

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

include/ns.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3667,7 +3667,7 @@ Ns_CtxSHAFinal(Ns_CtxSHA1 *ctx, unsigned char digest[20])
36673667

36683668
NS_EXTERN char *
36693669
Ns_HexString(const unsigned char *octets, char *outputBuffer, TCL_SIZE_T size, bool isUpper)
3670-
NS_GNUC_NONNULL(1) NS_GNUC_NONNULL(2) NS_GNUC_PURE;
3670+
NS_GNUC_NONNULL(1,2);
36713671

36723672
/*
36733673
* tclrequest.c:

nsd/tclcrypto.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ static void ListMDfunc(const EVP_MD *m, const char *from, const char *to, void *
113113
static BIO *PEMOpenReadSteam(const char *fnOrData)
114114
NS_GNUC_NONNULL(1);
115115

116-
static void uuid_format(unsigned char *b, char *dst) NS_GNUC_NONNULL(1,2) NS_GNUC_PURE;
117-
static int uuid_v4(char *dst) NS_GNUC_NONNULL(1);
118-
static int uuid_v7(char *dst) NS_GNUC_NONNULL(1);
116+
static char *uuid_format(unsigned char *b, char *dst) NS_GNUC_NONNULL(1,2) NS_GNUC_PURE;
117+
static const char *uuid_v4(char *dst) NS_GNUC_NONNULL(1);
118+
static const char *uuid_v7(char *dst) NS_GNUC_NONNULL(1);
119119

120120
static TCL_OBJCMDPROC_T CryptoHmacAddObjCmd;
121121
static TCL_OBJCMDPROC_T CryptoHmacFreeObjCmd;
@@ -3442,7 +3442,7 @@ NsTclCryptoRandomBytesObjCmd(ClientData UNUSED(clientData), Tcl_Interp *interp,
34423442
*----------------------------------------------------------------------
34433443
*/
34443444

3445-
static inline void
3445+
static inline char *
34463446
uuid_format(unsigned char *b, char *dst)
34473447
{
34483448
/*
@@ -3462,6 +3462,8 @@ uuid_format(unsigned char *b, char *dst)
34623462

34633463
Ns_HexString(&b[10], &dst[24], 6, NS_FALSE); /* bytes 10..15 -> dst[24..35] */
34643464
dst[36] = '\0';
3465+
3466+
return dst;
34653467
}
34663468

34673469
/*
@@ -3488,13 +3490,13 @@ uuid_format(unsigned char *b, char *dst)
34883490
*
34893491
*----------------------------------------------------------------------
34903492
*/
3491-
static inline int
3493+
static inline const char *
34923494
uuid_v4(char *dst)
34933495
{
34943496
unsigned char b[16];
34953497

34963498
if (RAND_bytes(b, sizeof(b)) != 1) {
3497-
return TCL_ERROR;
3499+
return NULL;
34983500
}
34993501

35003502
/*
@@ -3507,8 +3509,7 @@ uuid_v4(char *dst)
35073509
b[6] = (unsigned char)((b[6] & 0x0F) | 0x40); /* version 4 */
35083510
b[8] = (unsigned char)((b[8] & 0x3F) | 0x80); /* variant RFC4122 */
35093511

3510-
uuid_format(b, dst);
3511-
return TCL_OK;
3512+
return uuid_format(b, dst);
35123513
}
35133514

35143515

@@ -3536,7 +3537,7 @@ uuid_v4(char *dst)
35363537
*
35373538
*----------------------------------------------------------------------
35383539
*/
3539-
static inline int
3540+
static inline const char *
35403541
uuid_v7(char *dst)
35413542
{
35423543
unsigned char out[16];
@@ -3550,7 +3551,7 @@ uuid_v7(char *dst)
35503551
* - parts of them go into the rand_a / rand_b fields.
35513552
*/
35523553
if (RAND_bytes(rnd, sizeof(rnd)) != 1) {
3553-
return TCL_ERROR;
3554+
return NULL;
35543555
}
35553556

35563557
/*
@@ -3608,9 +3609,7 @@ uuid_v7(char *dst)
36083609
* The rest (bytes 9..15) are just rnd[3]..rnd[9]
36093610
*/
36103611
memcpy(&out[9], &rnd[3], 7);
3611-
uuid_format(out, dst);
3612-
3613-
return TCL_OK;
3612+
return uuid_format(out, dst);
36143613
}
36153614

36163615
/*
@@ -3649,19 +3648,22 @@ NsTclCryptoUUIDObjCmd(ClientData UNUSED(clientData), Tcl_Interp *interp, TCL_SIZ
36493648

36503649
} else {
36513650
Tcl_DString ds;
3651+
const char* hexString;
36523652

36533653
Tcl_DStringInit(&ds);
36543654
Tcl_DStringSetLength(&ds, (TCL_SIZE_T)36);
36553655

36563656
if (versionInt == 4) {
3657-
result = uuid_v4(ds.string);
3657+
hexString = uuid_v4(ds.string);
36583658
} else {
3659-
result = uuid_v7(ds.string);
3659+
hexString = uuid_v7(ds.string);
36603660
}
3661-
if (result == TCL_OK) {
3661+
if (hexString != NULL) {
36623662
Tcl_DStringResult(interp, &ds);
3663+
result = TCL_OK;
36633664
} else {
36643665
Ns_TclPrintfResult(interp, "UUID conversion failed");
3666+
result = TCL_ERROR;
36653667
}
36663668
}
36673669

0 commit comments

Comments
 (0)