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

call to getenv(String) does not terminate the string return value #27

Open
seanmceligot opened this issue Oct 3, 2014 · 2 comments
Open

Comments

@seanmceligot
Copy link

The following code returns not just the USERNAME environment variable, but also every environment after that.
myusername USERPROFILE=C:\Users\myusername ...

Similar code used to work with jna. This is on Windows 7.

import jnr.ffi.provider.FFIProvider;
import jnr.posix.util.Platform;

public class NativeCallsTest {
   public interface CLibrary  {
         public int system(String cmd);
         public String getenv(String name);
}
public static void main(String[] args) {
  CLibrary _libc;

    jnr.ffi.LibraryLoader<CLibrary> loader = FFIProvider.getSystemProvider().createLibraryLoader(CLibrary.class);
    _libc = loader.load(Platform.IS_WINDOWS ? "msvcrt" : "c");
    System.out.println(_libc.getenv("USERNAME"));
  }
}
@headius
Copy link
Member

headius commented Oct 3, 2014

Well that is indeed peculiar! It is especially strange because I would not expect USERNAME passed to getenv to return anything but the value bound to USERNAME. Perhaps there's some pecular behavioral difference in the Windows implementation of getenv that we're not handling right?

@headius
Copy link
Member

headius commented Sep 26, 2016

A theory: getenv on Windows returns a pointer into an existing ENV string that has nulls in it; C would see a null before the next env var and not include it in the string.

Does the bad string contain null characters? I can't tell from your example.

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