Disclaimer: I am not a lawyer.
But what you suggest is fine unless the code has been patented. That is very unlikely, of course.
Distinguish between using something (permitted - unless patented) and republishing it (depends on license).
The only time in which you can't use something is when it has been patented but not licensed to you. I doubt that is the case here. But otherwise you can use what you find and you can use modifications of it. You just can't republish the work of others without some sort of permission such as a license.
Plagiarism is a different issue than publishing/copyright. If you cite the work you are correctly attributing it to the source and so avoid plagiarism.
Think of an analogy with mathematics. If I find a (copyright) formula in a math book, I can use it. I can even adapt it for other use. I can't claim that I created it (that would be plagiarism) and I can't republish it without permission. If this were not the case it would be impossible for mathematics to advance. Every formula or other idea would be an absolute block to every other mathematician.
Think of an analogy with poetry. If I find a poem published somewhere, I can adapt and rewrite it for my own purposes. I just can't republish it and I may not be able to publish my rewrite. But I can use it myself, perhaps for my own enjoyment.
Code is a bit different since it can be executed. If I find it and it isn't patented, I can execute it. I can also rewrite it for my own purposes. And I can execute that also. I just can't republish it necessarily. And whether I can publish the changed code depends on things.
For reproducibility purposes you can publish how you changed the original because that is your work, but you may not be able to publish the complete version you used since it is in part the work of another, but that depends on the license.
Patenting code is extremely problematic in my view. Unfortunately it is possible to do it in some places.
Note that I've assumed that the program code is published as text, not as an executable. I've also assumed that you have legal access to that text.