Skip to content

sort/sorthp/circshift don't validate ndarray sort-order/shift arg shape, should throw but don't #15658

Description

@manit2004

Description

sort, sorthp, and circshift all take an optional ndarray arg that's supposed to apply across all dims (sortOrder for sort/sorthp, k for circshift). It's supposed to be 0-d when you don't pass dims, but the code never actually checks that; it just assumes it and passes it straight to base():

if ( isndarrayLike( o ) ) {
    // As the operation is performed across all dimensions, `o` is assumed to be a zero-dimensional ndarray...
    return base( x, o );
}

For sort at L180.

Same thing happens on the fn(x, arg, {}) path (options with no dims key). So if you pass a wrong-shaped array it just... doesn't throw, and gets passed into base unchecked.

Found this because the tests for it are already there and already failing ("...is not broadcast-compatible" in each package's test.js)

make test-javascript TESTS_FILTER=".*/blas/ext/sort/test/test\.js"
make test-javascript TESTS_FILTER=".*/blas/ext/sorthp/test/test\.js"
make test-javascript TESTS_FILTER=".*/blas/ext/circshift/test/test\.js"

Related Issues

None that I found.

Questions

No.

Demo

No response

Reproduction

var zeros = require( '@stdlib/ndarray/zeros' );
var sort = require( '@stdlib/blas/ext/sort' );

var x = zeros( [ 2, 2 ], { 'dtype': 'generic' } );
var sortOrder = zeros( [ 4 ], { 'dtype': 'generic' } );

sort( x, sortOrder ); // should throw, doesn't

Same deal with circshift:

var circshift = require( '@stdlib/blas/ext/circshift' );

var x = zeros( [ 2, 2 ], { 'dtype': 'int32' } );
var k = zeros( [ 4 ], { 'dtype': 'int32' } );

circshift( x, k ); // should throw, doesn't

Expected Results

Throws — this is already what the existing tests assert.

Actual Results

Returns normally, no error, wrong-shaped array just gets used.

Version

0.4.1

Environments

Node.js

Browser Version

No response

Node.js / npm Version

Node v22.22.2, npm 10.9.7

Platform

Linux

Checklist

  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions