Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improper use of strerror_r in native/dispatch.c #215

Closed
LionelCons opened this issue Apr 8, 2013 · 2 comments
Closed

Improper use of strerror_r in native/dispatch.c #215

LionelCons opened this issue Apr 8, 2013 · 2 comments

Comments

@LionelCons
Copy link

In native/dispatch.c, we have:

#define STR_ERROR(CODE,BUF,LEN) (strerror_r(CODE, BUF, LEN), BUF)

and then later:

  if (error) {
    char emsg[1024];
    snprintf(msg, sizeof(msg), "[%d]%s", error, STR_ERROR(error, emsg, sizeof(emsg)));
    throw_type = ELastError;
    throw_msg = msg;
  }

However, strerror_r exists in two very different flavors: XSI and GNU, see for instance http://unixhelp.ed.ac.uk/CGI/man-cgi?strerror.

When the GNU flavor is used, the emsg buffer will maybe not be written to and the code will therefore use uninitialized data. This is exactly what we see in practice in our Java code on Linux 32-bits with errors like:

rename(x1, x2): [2]<?L
mkdir(d1): [17]?*??_?�x?

FWIW, the Linux man page contains:

   The XSI-compliant version of strerror_r() is provided if:
   (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
   Otherwise, the GNU-specific version is provided.

So either the code should force the use of the XSI version of strerror_r or it should conditionally define STR_ERROR to work in both cases.

@LionelCons
Copy link
Author

Here is a sample program to show the issue and its output on Linux 32-bits. Other systems/architectures may work better.

import com.sun.jna.LastErrorException;
import com.sun.jna.Library;
import com.sun.jna.Native;
class jna215
{  
    public interface POSIX extends Library {
        public int rmdir(String path) throws LastErrorException;
    }
    public static void main(String args[])
    {
        POSIX posix = (POSIX) Native.loadLibrary("c", POSIX.class);
        try {
            posix.rmdir("/a/b/c/d");
        } catch (LastErrorException e) {
            System.out.println(e.getMessage());
        }
        try {
            posix.rmdir("/a/b/c");
        } catch (LastErrorException e) {
            System.out.println(e.getMessage());
        }
    }
}

Output:

[2]
[2]¬ùy·

@twall
Copy link
Contributor

twall commented Apr 8, 2013

Addressed in a676e3a, but need to verify no other side effects from the addition of _XOPEN_SOURCE.

@twall twall closed this as completed May 7, 2013
mstyura pushed a commit to mstyura/jna that referenced this issue Sep 9, 2024
Motivation:

To make it easier to reuse the ChannelFutureListener in higher level protocols we should just use Channel.Unsafe directly to write the FIN, otherwise each handler in the pipeline will need to handle a QuicStreamFrame which makes things harder.

Modifications:

Use Unsafe directly to write the FIN

Result:

Easier to reuse the listener
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants