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

Tempfile mangels flags #3913

Closed
pruy opened this issue May 23, 2016 · 1 comment
Closed

Tempfile mangels flags #3913

pruy opened this issue May 23, 2016 · 1 comment

Comments

@pruy
Copy link

pruy commented May 23, 2016

Environment

JRuby 1.7x (bundled with sonarqube)
Detected with jruby-complete-1.7.9.jar
Sill detectable with jruby-complete-1.7.25.jar

Platform: NetBSD 7.0_STABLE

Expected Behavior

Open modes stay constant.

Actual Behavior

Open modes get mangled

Analysis

(code shown is from jruby 1.7.25)

TempfileClallback.call() method initializes ioOptions :

IOOptions ioOptions = newIOOptions(runtime, ModeFlags.RDWR | ModeFlags.EXCL);

In may case no further modifications of this value occurs.
This value is passed some lines down:

initializeOpen(ioOptions);

This method looks as follows:

private void initializeOpen(IOOptions ioOptions) {
   getRuntime().getPosix().chmod(path, 0600);
    MakeOpenFile();

   openFile.setMode(ioOptions.getModeFlags().getOpenFileFlags());
    openFile.setPath(path);

    sysopenInternal19(path, ioOptions.getModeFlags().getOpenFileFlags(), 0600);
}

However, sysopeninternal19 (from RubyFile) is creating a new ModeFlags instance from the second parameter (named oflags) there.

ModeFlags modes = ModeFlags.createModeFlags(oflags);

Now, javadoc for getOpenFileFlags() states:

/**
 * Convert the flags in this object to a set of flags appropriate for the
 * OpenFile structure and logic therein.
 * 
 * @return an int of flags appropriate for OpenFile
 */    

On the other hand the ModeFlags(int) constructor just uses the integer passed in
for the internal flags value. Those flags eare interpreted according to OpenFlags
constants. Thos are not (necessarily) compatible with flags according to OpenFile

This causes wrong open modes for the resulting file.

This bug is subtle as some openmodes may end up compatible.

E.g. for most platforms OpenFlags modes are (interesting part)

RDONLY 0
WRONLY 1
RDWR   2

While ÒpenFiles` has

public static final int READABLE           = 0x00000001;
public static final int WRITABLE           = 0x00000002;
public static final int READWRITE          = READABLE | WRITABLE;

In case of RDWR getOpenFileFlags() returns 2 (WRITABLE).
Fed into the constructor this ends up as being RDWRagain.
If you are not going to read the file (despite setting RDWR the effect will get by unnoticed.

Proposed Fix

change

        sysopenInternal19(path, ioOptions.getModeFlags().getOpenFileFlags(), 0600);

to

        sysopenInternal19(path, ioOptions.getModeFlags().getFlags(), 0600);
@kares
Copy link
Member

kares commented Sep 25, 2018

since 1.7 is EOL and 9K has a redone IO sub-system I'd close this one.
if anyone feels like the proposal is worth doing - please submit a PR it will likely get accepted.

@kares kares closed this as completed Sep 25, 2018
@kares kares added this to the Invalid or Duplicate milestone Sep 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants