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

Code before:
                    vector[j].u.score = valkey_strtod(byval->ptr, &eptr);
                    if (eptr[0] != '\0' || errno == ERANGE || errno == EINVAL || isnan(vector[j].u.score)) {
                        int_conversion_error = 1;
                    }
                } else if (byval->encoding == OBJ_ENCODING_INT) {
                    /* Don't need to decode the object if it's
                     * integer-encoded (the only encoding supported) so
                     * far. We can just cast it */
                    vector[j].u.score = (long)byval->ptr;
                } else {
                    serverAssertWithInfo(c, sortval, 1 != 1);
                }
            }

            /* when the object was retrieved using lookupKeyByPattern,
             * its refcount needs to be decreased. */
            if (sortby) {
                decrRefCount(byval);
            }
        }

        server.sort_desc = desc;
        server.sort_alpha = alpha;
        server.sort_bypattern = sortby ? 1 : 0;
        server.sort_store = storekey ? 1 : 0;
        /* If the source keys are empty, we have will have a vector length of zero,
         * so no need to sort. */
        if (vectorlen != 0) {   
            if (sortby && (start != 0 || end != vectorlen - 1))
                pqsort(vector, vectorlen, sizeof(serverSortObject), sortCompare, start, end);
            else
                qsort(vector, vectorlen, sizeof(serverSortObject), sortCompare);
        }
    }

    /* Send command output to the output buffer, performing the specified
     * GET/DEL/INCR/DECR operations if any. */
    outputlen = getop ? getop * (end - start + 1) : end - start + 1;
    if (int_conversion_error) {
        addReplyError(c, "One or more scores can't be converted into double");
    } else if (storekey == NULL) {
        /* STORE option not specified, sent the sorting result to client */
        addReplyArrayLen(c, outputlen);
        for (j = start; j <= end; j++) {
            listNode *ln;
            listIter li;

            if (!getop) addReplyBulk(c, vector[j].obj);
            listRewind(operations, &li);
            while ((ln = listNext(&li))) {
                serverSortOperation *sop = ln->value;

Review comment (suggestion):
```suggestion
        /* If the source keys are empty, we will have a vector length of zero,
```

Code after:
                    vector[j].u.score = valkey_strtod(byval->ptr, &eptr);
                    if (eptr[0] != '\0' || errno == ERANGE || errno == EINVAL || isnan(vector[j].u.score)) {
                        int_conversion_error = 1;
                    }
                } else if (byval->encoding == OBJ_ENCODING_INT) {
                    /* Don't need to decode the object if it's
                     * integer-encoded (the only encoding supported) so
                     * far. We can just cast it */
                    vector[j].u.score = (long)byval->ptr;
                } else {
                    serverAssertWithInfo(c, sortval, 1 != 1);
                }
            }

            /* when the object was retrieved using lookupKeyByPattern,
             * its refcount needs to be decreased. */
            if (sortby) {
                decrRefCount(byval);
            }
        }

        server.sort_desc = desc;
        server.sort_alpha = alpha;
        server.sort_bypattern = sortby ? 1 : 0;
        server.sort_store = storekey ? 1 : 0;
        /* If the source keys are empty, we will have a vector length of zero,
         * so no need to sort. */
        if (vectorlen != 0) {
            if (sortby && (start != 0 || end != vectorlen - 1))
                pqsort(vector, vectorlen, sizeof(serverSortObject), sortCompare, start, end);
            else
                qsort(vector, vectorlen, sizeof(serverSortObject), sortCompare);
        }
    }

    /* Send command output to the output buffer, performing the specified
     * GET/DEL/INCR/DECR operations if any. */
    outputlen = getop ? getop * (end - start + 1) : end - start + 1;
    if (int_conversion_error) {
        addReplyError(c, "One or more scores can't be converted into double");
    } else if (storekey == NULL) {
        /* STORE option not specified, sent the sorting result to client */
        addReplyArrayLen(c, outputlen);
        for (j = start; j <= end; j++) {
            listNode *ln;
            listIter li;

            if (!getop) addReplyBulk(c, vector[j].obj);
            listRewind(operations, &li);
            while ((ln = listNext(&li))) {
                serverSortOperation *sop = ln->value;