X Tutup
Skip to content

How to get error code in safe way if error occurred when read/write data from pcm node? #251

@joe820730

Description

@joe820730

When error occurred, we can get human readable error message with pcm_get_error(), which might like this:

if (pcm_read(pcm) < 0) {
    fprintf(stderr, "Error: %s\n", pcm_get_error(pcm));
}

In general we'll do error handling according to errno, so the code might like this:

// For example, we will keep retry if device is busy, or close if no such device.
while (running) {
    if (pcm_read(pcm) < 0) {
        int err = errno;
        if (ENODEV == err) {
            break;
        } else if (EBUSY == err) {
            continue;
        } else if ( ...... ) {
            // Other error handling
        }
    }
    // Do something if we got data from pcm device
}

Then I found the error message was generated by oops() in pcm.c, in this function it will call vsnprintf() and snprintf().
But the description about fprintf series in POSIX standard:
If an output error was encountered, these functions shall return a negative value and set errno to indicate the error.
Also linux man page said:

A common mistake is to do
if (somecall() == -1) {
    printf("somecall() failed\n");
    if (errno == ...) { ... }
}
where errno no longer needs to have the value it had upon return from somecall()

It means that we can't make sure the errno can reflect to the correct error case, because it might overwrited if error occurred when calling oops().
If so, how to get correct error code when error occurred when operating with pcm device?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup