mithun218 / GiNaCDE

GiNaC Differential Equation Solver
MIT License
9 stars 1 forks source link

Segmentation fault when `degAcoeff` is not set #13

Closed peanutfun closed 2 years ago

peanutfun commented 3 years ago

Not setting degAcoeff results in a segmentation fault when trying to solve an equation with the (modified) F-expansion method. The error occurs at desolve.cpp#L681.

Example code taken from examples/5thGKdV.cpp:

#include <GiNaCDE.h>

int main()
{
  const ex u = reader("u"), t = reader("t"), x = reader("x"),
           k_0 = reader("k_0"), k_1 = reader("k_1"), A_1 = reader("A_1"),
           A_2 = reader("A_2"), A_3 = reader("A_3"), p = reader("p"),
           q = reader("q"), r = reader("r");

  const ex pde = Diff(u, t, 1) + p * u * Diff(u, x, 3) +
                 q * Diff(u, x, 1) * Diff(u, x, 2) +
                 r * pow(u, 2) * Diff(u, x, 1) + Diff(u, x, 5);

  depend(u, { t, x });

  // F-expansion method//
  twcPhase = lst{ lst{ k_0, k_1 }, lst{} };
  // degAcoeff = lst{ 3, 0, A_1, A_2, A_3 };  // This line commented out
  ASolve = false;
  positivePart = true;
  negativePart = false;
  paraInDiffSolve = lst{ r };
  filename = "5thGKdV_Fexp.txt";
  desolve(pde, { u }, F_expansion);  // Segfault

  return 0;
}

In command line:

$ ./7thorder
[1] 8371 segmentation fault  ./7thorder

As a remedy, I suggest checking if degAcoeff is not default-initialized anymore before accessing it, and throwing an exception otherwise. This way, users also get notified of what went wrong.

mithun218 commented 2 years ago

@peanutfun Thanks for opening this issue. I will open a new PR to resolve this problem according to your suggestion.

mithun218 commented 2 years ago

@peanutfun I have implemented your suggestion here. Now GiNaCDE can check the correct initialization of degAcoeff before accessing it and shows a message if degAcoeff is not initialized correctly.

peanutfun commented 2 years ago

Yes, I can confirm that the issue is resolved. Thank you!