Skip to content

Fix use-after-move cases - #3390

Open
andrjohns wants to merge 1 commit into
developfrom
cols-dotprog-seg
Open

Fix use-after-move cases#3390
andrjohns wants to merge 1 commit into
developfrom
cols-dotprog-seg

Conversation

@andrjohns

@andrjohns andrjohns commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

There were two cases in the Math library which used a matrix input after std::forward-ing it into a ref or an arena type. This wasn't an issue unless rvalues were passed as inputs, at which point the ref/arena would move-construct and leave an empty matrix:

#include <stan/math.hpp>
#include <iostream>

int main() {
  Eigen::MatrixXd x(1, 1);
  x << 2.0;
  Eigen::Matrix<stan::math::var, -1, -1> y(1, 1);
  y << 0.5;
  auto r = stan::math::columns_dot_product(std::move(x), y); //segfaults
  std::cout << r(0).val() << std::endl; 
  return 0;
}

I (and claude) could only find this in the rev overloads for cholesky_decompose and columns_dot_product, but not sure if there's a good way to add a more universal test for this.

Tests

N/A - tests should still pass

Side Effects

N/A

Release notes

Fix use-after-move in cholesky_decompose and columns_dot_product

Checklist

  • Copyright holder: (fill in copyright holder information)

    The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
    - Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
    - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

  • the basic tests are passing

    • unit tests pass (to run, use: ./runTests.py test/unit)
    • header checks pass, (make test-headers)
    • dependencies checks pass, (make test-math-dependencies)
    • docs build, (make doxygen)
    • code passes the built in C++ standards checks (make cpplint)
  • the code is written in idiomatic C++ and changes are documented in the doxygen

  • the new changes are tested

@WardBrian

Copy link
Copy Markdown
Member

I (and claude) could only find this in the rev overloads for cholesky_decompose and columns_dot_product, but not sure if there's a good way to add a more universal test for this.

I think we could extend the test generation we use for expressions to generate calls with temporaries, maybe? Could not be worth the hassle

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants