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

Code before:
        size_t buf_size;
        char *buf = zmalloc_usable(min_size, &buf_size);
        sdswrite(buf, field_size, field_sds_type, field, field_len);
        sdswrite(buf + field_size, buf_size - field_size, SDS_TYPE_8, value, value_len);
        embedded_field_sds = (sds)(buf + sdsHdrSize(field_sds_type));
        /* Field sds aux bits are zero, which we use for this entry encoding. */
        sdsSetAuxBit(embedded_field_sds, FIELD_SDS_AUX_BIT_ENTRY_HAS_PTR_VALUE, 0);
        serverAssert(entryGetEncoding(embedded_field_sds) == ENTRY_ENC_EMB_VALUE);
        sdsfree(value);
    } else {
        /* Embed field, but not value. Field must be >= SDS_TYPE_8 to encode to
         * indicate this type of entry.
         *
         *     +-------+---------------+
         *     | value | field         |
         *     | ptr   | hdr8 "foo" \0 |
         *     +-------+---------------+
         */
        char field_sds_type = sdsReqType(field_len);
        if (field_sds_type == SDS_TYPE_5) field_sds_type = SDS_TYPE_8;
        field_size = sdsReqSize(field_len, field_sds_type);
        size_t alloc_size = sizeof(void *) + field_size;
        hashTypeEntryPtrValue *entry = zmalloc(alloc_size);
        entry->value = value;
        sdswrite(entry->field_data, field_size, field_sds_type, field, field_len);
        embedded_field_sds = (sds)(entry->field_data + sdsHdrSize(field_sds_type));
        /* Store the entry encoding type in sds aux bits. */
        sdsSetAuxBit(embedded_field_sds, FIELD_SDS_AUX_BIT_ENTRY_HAS_PTR_VALUE, 1);
        serverAssert(entryGetEncoding(embedded_field_sds) == ENTRY_ENC_PTR_VALUE);
    }
    return (void *)embedded_field_sds;
}

/* The entry pointer is the field sds, but that's an implementation detail. */
sds hashTypeEntryGetField(const hashTypeEntry *entry) {
    return (sds)entry;
}

sds hashTypeEntryGetValue(const hashTypeEntry *entry) {
    switch (entryGetEncoding(entry)) {
    case ENTRY_ENC_EMB_VALUE: {
        /* Skip field content, field null terminator and value sds8 hdr. */
        size_t offset = sdslen(entry) + 1 + sdsHdrSize(SDS_TYPE_8);
        char *buf = (char *)entry + offset;
        return buf;
    }
    case ENTRY_ENC_PTR_VALUE: {
        const hashTypeEntryPtrValue *entry_struct = getEntryStruct(entry);
        return entry_struct->value;
    }
    default:

Review comment (suggestion):
```suggestion
        size_t alloc_size = sizeof(value) + field_size;
        char *buf = zmalloc(alloc_size);
        memcpy(buf, &value, sizeof(value));
        embedded_field_sds = sdswrite(buf + sizeof(value), field_size, field_sds_type, field, field_len);
```

Code after:
        embedded_field_sds = sdswrite(buf, field_size, field_sds_type, field, field_len);
        sdswrite(buf + field_size, buf_size - field_size, SDS_TYPE_8, value, value_len);
        /* Field sds aux bits are zero, which we use for this entry encoding. */
        sdsSetAuxBit(embedded_field_sds, FIELD_SDS_AUX_BIT_ENTRY_HAS_VALUE_PTR, 0);
        serverAssert(!entryHasValuePtr(embedded_field_sds));
        sdsfree(value);
    } else {
        /* Embed field, but not value. Field must be >= SDS_TYPE_8 to encode to
         * indicate this type of entry.
         *
         *     +-------+---------------+
         *     | value | field         |
         *     | ptr   | hdr8 "foo" \0 |
         *     +-------+---------------+
         */
        char field_sds_type = sdsReqType(field_len);
        if (field_sds_type == SDS_TYPE_5) field_sds_type = SDS_TYPE_8;
        field_size = sdsReqSize(field_len, field_sds_type);
        size_t alloc_size = sizeof(sds *) + field_size;
        char *buf = zmalloc(alloc_size);
        *(sds *)buf = value;
        embedded_field_sds = sdswrite(buf + sizeof(sds *), field_size, field_sds_type, field, field_len);
        /* Store the entry encoding type in sds aux bits. */
        sdsSetAuxBit(embedded_field_sds, FIELD_SDS_AUX_BIT_ENTRY_HAS_VALUE_PTR, 1);
        serverAssert(entryHasValuePtr(embedded_field_sds));
    }
    return (void *)embedded_field_sds;
}

/* The entry pointer is the field sds, but that's an implementation detail. */
sds hashTypeEntryGetField(const hashTypeEntry *entry) {
    return (sds)entry;
}

sds hashTypeEntryGetValue(const hashTypeEntry *entry) {
    if (entryHasValuePtr(entry)) {
        return *hashTypeEntryGetValueRef(entry);
    } else {
        /* Skip field content, field null terminator and value sds8 hdr. */
        size_t offset = sdslen(entry) + 1 + sdsHdrSize(SDS_TYPE_8);
        return (char *)entry + offset;
    }
}

/* Returns the address of the entry allocation. */
static void *hashTypeEntryAllocPtr(hashTypeEntry *entry) {
    char *buf = sdsAllocPtr(entry);
    if (entryHasValuePtr(entry)) {
        buf -= sizeof(sds *);
    }
    return buf;