A ruling by the Supreme Judicial Court on Monday is expected to help untangle parts of the sentencing mess left by the mishandling of drug samples by a state chemist that might have tainted tens of thousands of criminal cases.

In the unanimous decision, justices ruled that defendants whose convictions were based on evidence tainted by the chemist, Annie Dookhan, can seek new trials without facing added charges or a more severe sentence.

“It clears a path for people to challenge — when I say people, I say thousands, maybe tens of thousands of people — to challenge their convictions without fear that prosecutors will respond by seeking to revive harsher charges or harsher sentences that were relinquished in a plea bargain,” said Matthew Segal, the legal director for the American Civil Liberties Union of Massachusetts.

Ms. Dookhan pleaded guilty in 2013 to 27 counts, including perjury, obstruction of justice and tampering with evidence, and was sentenced to three to five years in prison plus two years of probation. Prosecutors said that during her nine years at a drug lab in Boston, where she processed drug samples seized from suspects, Ms. Dookhan failed to properly test samples before declaring them positive, mixed up samples and forged signatures and lied about her credentials. Ms. Dookhan’s lawyer said she was trying to be the best chemist on staff.