Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ final class Installation {

private static final Charset UTF_8 = Charset.forName("UTF-8");

// The id is a UUID, so a valid file is far below this
private static final int MAX_INSTALLATION_FILE_SIZE = 1024;

static final @NotNull AutoClosableReentrantLock staticLock = new AutoClosableReentrantLock();

private Installation() {}
Expand All @@ -43,20 +46,33 @@ public static String id(final @NotNull Context context) throws RuntimeException
return deviceId;
}
deviceId = readInstallationFile(installation);
} catch (Throwable e) {
if (deviceId == null) {
deviceId = writeInstallationFile(installation);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return deviceId;
}
}

/**
* Returns null when the file is larger than an id can be, or can't be read, so the caller treats
* it as corrupt. The size check keeps an oversized file from being read into memory.
*/
@TestOnly
static @NotNull String readInstallationFile(final @NotNull File installation) throws IOException {
static @Nullable String readInstallationFile(final @NotNull File installation) {
try (final RandomAccessFile f = new RandomAccessFile(installation, "r")) {
final byte[] bytes = new byte[(int) f.length()];
final long length = f.length();
if (length > MAX_INSTALLATION_FILE_SIZE) {
return null;
}
final byte[] bytes = new byte[(int) length];
f.readFully(bytes);
return new String(bytes, UTF_8);
} catch (IOException e) {
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.sentry.ktorClient

import io.ktor.client.request.HttpRequest
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsBytes
import io.ktor.http.Headers
import io.ktor.http.contentLength
import io.ktor.util.toMap
Expand All @@ -22,7 +21,7 @@ import io.sentry.util.UrlUtils
import java.util.concurrent.TimeUnit

internal object SentryKtorClientUtils {
internal suspend fun captureClientError(
internal fun captureClientError(
scopes: IScopes,
request: HttpRequest,
response: HttpResponse,
Expand Down Expand Up @@ -50,9 +49,8 @@ internal object SentryKtorClientUtils {
cookies = CookieUtils.filterSetCookie(response.headers["Set-Cookie"], scopes.options)
headers = getResponseHeaders(scopes, response.headers)
statusCode = response.status.value
try {
bodySize = response.bodyAsBytes().size.toLong()
} catch (_: Throwable) {}
// Content-Length rather than reading the body, which would copy all of it into memory
bodySize = response.contentLength()
}

event.request = sentryRequest
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/cache/tape/QueueFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ boolean ringRead(long position, byte[] buffer, int offset, int count) throws IOE
resetFile();
} catch (IOException e) {
throw e;
} catch (Throwable e) {
} catch (IndexOutOfBoundsException e) {
// most likely the file is corrupt, so we delete it and recreate, accepting data loss
resetFile();
}
Expand Down
Loading