Skip to content

Commit

Permalink
Do not expose mutable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Apr 18, 2024
1 parent cdd9109 commit 608e04a
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public RobotCaptureFactory(boolean allScreens) {

@Override
public Dimension getDimension() {
return captureDimension;
return new Dimension(captureDimension);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static synchronized void setShareAllScreens(boolean doShareAllScreens) {
}

public static Rectangle getSharedScreenSize() {
return sharedScreenSize;
return new Rectangle(sharedScreenSize);
}

public static int getNumberOfScreens() {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/mpo/dayon/common/buffer/MemByteBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* A mixed between a byte buffer and a byte stream ...
*/
public class MemByteBuffer extends OutputStream {
public class MemByteBuffer extends OutputStream implements Cloneable {
private static final int DEFAULT_INITIAL_CAPACITY = 32;

private byte[] buffer;
Expand Down Expand Up @@ -127,4 +127,13 @@ private void increaseBuffer(int newCount) {
buffer = Arrays.copyOf(buffer, Math.max(buffer.length << 1, newCount));
}
}

@Override
public final MemByteBuffer clone() {
try {
return (MemByteBuffer) super.clone();
} catch (CloneNotSupportedException e) {
return new MemByteBuffer(buffer);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/mpo/dayon/common/capture/Capture.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public Capture(int captureId, boolean reset, int skipped, int merged, Dimension
this.reset = reset;
this.skipped = new AtomicInteger(skipped);
this.merged = new AtomicInteger(merged);
this.captureDimension = captureDimension;
this.tileDimension = tileDimension;
this.captureDimension = new Dimension(captureDimension);
this.tileDimension = new Dimension(tileDimension);
this.dirty = dirty.clone();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mpo/dayon/common/capture/CaptureTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public CaptureTile(int captureId, int id, XYWH xywh, MemByteBuffer capture) {
this.position = new Position(xywh.x, xywh.y);
this.width = xywh.w;
this.height = xywh.h;
this.capture = capture;
this.capture = capture.clone();
if (width * height != capture.size()) {
throw new IllegalArgumentException("Ouch!");
}
Expand Down Expand Up @@ -149,7 +149,7 @@ public int getHeight() {
}

public MemByteBuffer getCapture() {
return capture;
return capture.clone();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mpo/dayon/common/network/TransferableImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TransferableImage implements Transferable, Serializable {
private transient BufferedImage image;

public TransferableImage(BufferedImage image) {
this.image = image;
this.image = image.getSubimage(0, 0, image.getWidth(), image.getHeight());
}

private void writeObject(ObjectOutputStream out) throws IOException {
Expand All @@ -42,6 +42,6 @@ public BufferedImage getTransferData(DataFlavor flavor) {
if (!DataFlavor.imageFlavor.equals(flavor)) {
Log.error(new UnsupportedFlavorException(flavor).getMessage());
}
return image;
return image.getSubimage(0, 0, image.getWidth(), image.getHeight());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static NetworkCaptureMessage unmarshall(ObjectInputStream in) throws IOEx
}

public MemByteBuffer getPayload() {
return payload;
return payload.clone();
}

public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class NetworkClipboardGraphicMessage extends NetworkMessage {
private final int size;

public NetworkClipboardGraphicMessage(TransferableImage payload) {
this.payload = payload;
this.payload = new TransferableImage(payload.getTransferData(DataFlavor.imageFlavor));
// this is just a rather rough estimation
this.size = payload.getTransferData(DataFlavor.imageFlavor).getData().getDataBuffer().getSize() * 4;
}
Expand All @@ -24,7 +24,7 @@ public static NetworkClipboardGraphicMessage unmarshall(ObjectInputStream in) th
}

public TransferableImage getGraphic() {
return payload;
return new TransferableImage(payload.getTransferData(DataFlavor.imageFlavor));
}

@Override
Expand Down

0 comments on commit 608e04a

Please sign in to comment.