Skip to content

Commit

Permalink
Ensure string-as-pointer is a mutable version
Browse files Browse the repository at this point in the history
If a String is passed into an FFI function as an "out" pointer, we
must ensure we use a mutable version. If the original string param
is frozen, it must be duped to a non-frozen version, and if it is
shared we must pre-modify it to avoid overwriting any shared
buffers.

This may not be the best way to handle this. My instinct was to
add logic into the "strategy" that can provide a mutable version
of a given parameter, but this is a smaller localized change. We
will want to revisit this logic and avoid type-specific logic in
the marshaller when it should live in the strategy.

Fixes jruby#8365
  • Loading branch information
headius committed Oct 10, 2024
1 parent 4765ec9 commit 15b5bed
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,18 @@ public final void marshal(ThreadContext context, InvocationBuffer buffer, IRubyO
buffer.putAddress(strategy.address(parameter));

} else {
if (ArrayFlags.isOut(flags)) {
if (parameter instanceof RubyString) {
RubyString parameterStr = (RubyString) parameter;
if (parameter.isFrozen()) {
parameter = parameterStr.strDup(context.runtime);
}

// ensure string is modifiable
parameterStr.modify();
}
}

buffer.putArray(byte[].class.cast(strategy.object(parameter)), strategy.offset(parameter), strategy.length(parameter),
flags);
}
Expand Down

0 comments on commit 15b5bed

Please sign in to comment.