linux-test-project / ltp

Linux Test Project (mailing list: https://lists.linux.it/listinfo/ltp)
https://linux-test-project.readthedocs.io/
GNU General Public License v2.0
2.28k stars 1k forks source link

fix undefined behavior. #922

Closed ihsinme closed 2 years ago

ihsinme commented 2 years ago

in your code, you don't initialize val and you don't check the result of running sscanf. therefore, in case of an incorrect call to sscanf, function get_num_switches may return a random value. the easiest fix is to initialize val to zero

pevik commented 2 years ago

Of course, code is correct, I just wonder under which circumstances could scanning of "%s %lu" in /proc/stat fail. Found by static analysis or really an error during testing?

FYI we prefer patches on ML: http://lists.linux.it/listinfo/ltp (https://github.com/linux-test-project/ltp#developers-corner).

richiejp commented 2 years ago

The change itself looks fine.

Really though, we should check the result of sscanf. Also I think this test needs to be converted to the new API.

ihsinme commented 2 years ago

Good afternoon. Sorry for the delay, could you elaborate on my next steps. Thanks to.

pevik commented 2 years ago

@richiejp going to merge this, can I add your Reviewed-by: tag? https://github.com/pevik/ltp/commit/3571296d225bd93b6fc049b89a49b90a10600133