From 0da8fc466d91d0f03fafcd5173d0ae5f6f302e4e Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Mon, 10 Mar 2025 10:07:21 +0100 Subject: [PATCH] os: auth: use strlen() for auth proto name length No need to explicitly hard-code strings lengths when we can use standard strlen(). Those code pathes are so cold that trying to spare a few cycled for an (usually inlined) strlen() doesn't seem to justify any extra care. Signed-off-by: Enrico Weigelt, metux IT consult --- os/auth.c | 18 +++++++----------- os/xdmcp.c | 3 ++- os/xdmcp.h | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/os/auth.c b/os/auth.c index b1011a7ec..b28e7308c 100644 --- a/os/auth.c +++ b/os/auth.c @@ -53,7 +53,6 @@ from The Open Group. #include "mitauth.h" struct protocol { - unsigned short name_length; const char *name; AuthAddCFunc Add; /* new authorization data */ AuthCheckFunc Check; /* verify client authorization data */ @@ -67,7 +66,6 @@ struct protocol { static struct protocol protocols[] = { { - .name_length = 18, .name = "MIT-MAGIC-COOKIE-1", .Add = MitAddCookie, .Check = MitCheckCookie, @@ -80,7 +78,6 @@ static struct protocol protocols[] = { }, #ifdef HASXDMAUTH { - .name_length = 19, .name = "XDM-AUTHORIZATION-1", .Add = XdmAddCookie, .Check = XdmCheckCookie, @@ -132,7 +129,7 @@ LoadAuthorization(void) while ((auth = XauReadAuth(f)) != 0) { for (i = 0; i < NUM_AUTHORIZATION; i++) { - if (protocols[i].name_length == auth->name_length && + if (strlen(protocols[i].name) == auth->name_length && memcmp(protocols[i].name, auth->name, (int) auth->name_length) == 0 && protocols[i].Add) { ++count; @@ -158,8 +155,7 @@ RegisterAuthorizations(void) int i; for (i = 0; i < NUM_AUTHORIZATION; i++) - XdmcpRegisterAuthorization(protocols[i].name, - (int) protocols[i].name_length); + XdmcpRegisterAuthorization(protocols[i].name); } #endif @@ -210,7 +206,7 @@ CheckAuthorization(unsigned int name_length, } if (name_length) { for (i = 0; i < NUM_AUTHORIZATION; i++) { - if (protocols[i].name_length == name_length && + if (strlen(protocols[i].name) == name_length && memcmp(protocols[i].name, name, (int) name_length) == 0) { return (*protocols[i].Check) (data_length, data, client, reason); @@ -244,7 +240,7 @@ AuthorizationFromID(XID id, for (i = 0; i < NUM_AUTHORIZATION; i++) { if (protocols[i].FromID && (*protocols[i].FromID) (id, data_lenp, datap)) { - *name_lenp = protocols[i].name_length; + *name_lenp = strlen(protocols[i].name); *namep = protocols[i].name; return 1; } @@ -260,7 +256,7 @@ RemoveAuthorization(unsigned short name_length, int i; for (i = 0; i < NUM_AUTHORIZATION; i++) { - if (protocols[i].name_length == name_length && + if (strlen(protocols[i].name) == name_length && memcmp(protocols[i].name, name, (int) name_length) == 0 && protocols[i].Remove) { return (*protocols[i].Remove) (data_length, data); @@ -276,7 +272,7 @@ AddAuthorization(unsigned name_length, const char *name, int i; for (i = 0; i < NUM_AUTHORIZATION; i++) { - if (protocols[i].name_length == name_length && + if (strlen(protocols[i].name) == name_length && memcmp(protocols[i].name, name, (int) name_length) == 0 && protocols[i].Add) { return (*protocols[i].Add) (data_length, data, FakeClientID(0)); @@ -297,7 +293,7 @@ GenerateAuthorization(unsigned name_length, int i; for (i = 0; i < NUM_AUTHORIZATION; i++) { - if (protocols[i].name_length == name_length && + if (strlen(protocols[i].name) == name_length && memcmp(protocols[i].name, name, (int) name_length) == 0 && protocols[i].Generate) { return (*protocols[i].Generate) (data_length, data, diff --git a/os/xdmcp.c b/os/xdmcp.c index 7a4e54ddf..54a5752e7 100644 --- a/os/xdmcp.c +++ b/os/xdmcp.c @@ -536,11 +536,12 @@ XdmcpRegisterAuthorizations(void) } void -XdmcpRegisterAuthorization(const char *name, int namelen) +XdmcpRegisterAuthorization(const char *name) { ARRAY8 authName; int i; + size_t namelen = strlen(name); authName.data = calloc(namelen, sizeof(CARD8)); if (!authName.data) return; diff --git a/os/xdmcp.h b/os/xdmcp.h index 45853c7bd..50f5f2d57 100644 --- a/os/xdmcp.h +++ b/os/xdmcp.h @@ -9,7 +9,7 @@ void XdmcpUseMsg(void); int XdmcpOptions(int argc, char **argv, int i); void XdmcpRegisterConnection(int type, const char *address, int addrlen); void XdmcpRegisterAuthorizations(void); -void XdmcpRegisterAuthorization(const char *name, int namelen); +void XdmcpRegisterAuthorization(const char *name); void XdmcpInit(void); void XdmcpReset(void); void XdmcpOpenDisplay(int sock);