Skip to content

Commit

Permalink
fix - Update the pipe path generation approach in Java (microsoft#1582)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiaaming authored Aug 13, 2024
1 parent 53e856c commit 7b31a06
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,28 @@ private String generateRandomPipeName() {
if (System.getProperty("os.name").startsWith("Windows")) {
return Paths.get("\\\\.\\pipe\\", generateRandomHex(16) + "-sock").toString();
}

int randomLength = 32;
int fixedLength = ".sock".length();
String tmpDir = System.getenv("XDG_RUNTIME_DIR");
if (tmpDir == null || tmpDir.isEmpty()) {
tmpDir = System.getProperty("java.io.tmpdir");
}
int fixedLength = ".sock".length();
int safeIpcPathLengths = 103;
int availableLength = safeIpcPathLengths - fixedLength - tmpDir.length();
int randomLength = 32;
int bytesLength = Math.min(availableLength / 2, randomLength);

if (bytesLength < 16) {
int limit = 0;
if (System.getProperty("os.name").startsWith("Mac")) {
limit = 103;
} else if (System.getProperty("os.name").startsWith("Linux")) {
limit = 107;
}
if (limit != 0){
randomLength = Math.min(limit - tmpDir.length() - fixedLength, randomLength);
}
if (randomLength < 16) {
throw new NamedPipeConnectionException("Unable to generate a random pipe name with character length less than 16");
}
return Paths.get(tmpDir, generateRandomHex(bytesLength) + ".sock").toString();

String randomSuffix = generateRandomHex(randomLength/2);
return Paths.get(tmpDir, randomSuffix + ".sock").toString();
}

private void sendImporterPipeName(String pipeName) {
Expand Down

0 comments on commit 7b31a06

Please sign in to comment.