Repository: valkey-io/valkey
File: src/acl.c (C)

Code before:
    struct ACLLogEntry *le = zmalloc(sizeof(*le));
    le->count = 1;
    le->reason = reason;
    le->username = sdsdup(username ? username : c->user->name);
    le->ctime = commandTimeSnapshot();
    le->entry_id = ACLLogEntryCount;
    le->timestamp_created = le->ctime;

    if (object) {
        le->object = object;
    } else {
        switch (reason) {
        case ACL_DENIED_CMD: le->object = sdsdup(c->cmd->fullname); break;
        case ACL_DENIED_KEY: le->object = sdsdup(c->argv[argpos]->ptr); break;
        case ACL_DENIED_CHANNEL: le->object = sdsdup(c->argv[argpos]->ptr); break;
        case ACL_DENIED_AUTH: le->object = sdsdup(c->argv[0]->ptr); break;
        default: le->object = sdsempty();
        }
    }

    /* if we have a real client from the network, use it (could be missing on module timers) */
    client *realclient = server.current_client ? server.current_client : c;

    le->cinfo = catClientInfoString(sdsempty(), realclient, 0);
    le->context = context;
    le->scripting_engine_name = scripting_engine_name ? sdsdup(scripting_engine_name) : sdsempty();

    /* Try to match this entry with past ones, to see if we can just
     * update an existing entry instead of creating a new one. */
    long toscan = 10; /* Do a limited work trying to find duplicated. */
    listIter li;
    listNode *ln;
    listRewind(ACLLog, &li);
    ACLLogEntry *match = NULL;
    while (toscan-- && (ln = listNext(&li)) != NULL) {
        ACLLogEntry *current = listNodeValue(ln);
        if (ACLLogMatchEntry(current, le)) {
            match = current;
            listDelNode(ACLLog, ln);
            listAddNodeHead(ACLLog, current);
            break;
        }
    }

    /* If there is a match update the entry, otherwise add it as a
     * new one. */
    if (match) {
        /* We update a few fields of the existing entry and bump the
         * counter of events for this entry. */
        sdsfree(match->cinfo);
        match->cinfo = le->cinfo;

Review comment (suggestion):
`scripting_engine_name` appears to be coming from globals, so do we really need to pass it as a parameter to `addACLLogEntry()`? Can't we just fetch it here, like this:

```suggestion
    le->scripting_engine_name = scriptIsRunning() ? scriptGetRunningEngineName() : sdsempty();
```

Code after:
    le->reason = reason;
    le->username = sdsdup(username ? username : c->user->name);
    le->ctime = commandTimeSnapshot();
    le->entry_id = ACLLogEntryCount;
    le->timestamp_created = le->ctime;

    if (object) {
        le->object = object;
    } else {
        switch (reason) {
        case ACL_DENIED_CMD: le->object = sdsdup(c->cmd->fullname); break;
        case ACL_DENIED_KEY: le->object = sdsdup(c->argv[argpos]->ptr); break;
        case ACL_DENIED_CHANNEL: le->object = sdsdup(c->argv[argpos]->ptr); break;
        case ACL_DENIED_AUTH: le->object = sdsdup(c->argv[0]->ptr); break;
        default: le->object = sdsempty();
        }
    }

    /* if we have a real client from the network, use it (could be missing on module timers) */
    client *realclient = server.current_client ? server.current_client : c;

    le->cinfo = catClientInfoString(sdsempty(), realclient, 0);

    if (context == ACL_LOG_CTX_SCRIPT &&
        strcmp(scriptGetRunningEngineName(), "LUA") == 0) {
        /* For backward compatibility, we track that it's Lua using a special
         * lua ACL log context. Any other scripting language is just "script" in
         * the ACL log. */
        context = ACL_LOG_CTX_LUA;
    }

    le->context = context;

    /* Try to match this entry with past ones, to see if we can just
     * update an existing entry instead of creating a new one. */
    long toscan = 10; /* Do a limited work trying to find duplicated. */
    listIter li;
    listNode *ln;
    listRewind(ACLLog, &li);
    ACLLogEntry *match = NULL;
    while (toscan-- && (ln = listNext(&li)) != NULL) {
        ACLLogEntry *current = listNodeValue(ln);
        if (ACLLogMatchEntry(current, le)) {
            match = current;
            listDelNode(ACLLog, ln);
            listAddNodeHead(ACLLog, current);
            break;
        }
    }

    /* If there is a match update the entry, otherwise add it as a