analogdevicesinc / libiio

A cross platform library for interfacing with local and remote Linux IIO devices
http://analogdevicesinc.github.io/libiio/
GNU Lesser General Public License v2.1
493 stars 318 forks source link

wrong error checks for result from iio_create_context() #1048

Open catkira opened 1 year ago

catkira commented 1 year ago

I did error checks for the result of iio_create_context like here https://github.com/analogdevicesinc/libiio/blob/9a82e3757208630886f6aff25f723d9ca95a9501/tests/iio_stresstest.c#L252C1-L268C4

However the problem was, that in case of an error a negative value gets return which was not caught by this 'if(!ctx)'.

I changed my code to

    ctx = iio_create_context(NULL, (std::string("ip:") + ip).c_str());
    int err = iio_err(ctx);
    if (err) {
        std::cerr << "Failed to create libiio context" << std::endl;
        ctx = nullptr;
        return;
    }

and then it worked correctly.

pcercuei commented 1 year ago

Thanks, can you send a PR? Since you already have the fix 😬