Skip to content
This repository has been archived by the owner on Nov 9, 2018. It is now read-only.

Commit

Permalink
Fixed crash in abblock and tp on file corruption
Browse files Browse the repository at this point in the history
Auditors: @bbondy
  • Loading branch information
SergeyZhukovsky committed Mar 1, 2016
1 parent 830602e commit dcba079
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ public ABPFilterParser(Context context) {
}
}

public boolean shouldBlockJava(String baseHost, String url, String filterOption) {
if (null == mBuffer) {
return false;
}

return shouldBlock(baseHost, url, filterOption);
}


public native void init(byte[] data);
public native String stringFromJNI();
public native boolean shouldBlock(String baseHost, String url, String filterOption);
private byte[] mBuffer;
private String mVerNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@ public static void removeOldVersionFiles(Context context, String fileName) {
}
}

public static byte[] readLocalFile(String path) {
public static byte[] readLocalFile(File path) {
byte[] buffer = null;

FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(path);
if (!path.exists()) {
return null;
}
inputStream = new FileInputStream(path.getAbsolutePath());
int size = inputStream.available();
buffer = new byte[size];
int n = - 1;
Expand All @@ -93,21 +96,21 @@ public static byte[] readLocalFile(String path) {
public static byte[] readData(Context context, String fileName, String urlString, String eTagPrepend, String verNumber,
boolean downloadOnly) {
File dataPath = new File(context.getApplicationInfo().dataDir, verNumber + fileName);
boolean fileExists = dataPath.exists();
long oldFileSize = dataPath.length();
EtagObject previousEtag = ADBlockUtils.getETagInfo(context, eTagPrepend);
long milliSeconds = Calendar.getInstance().getTimeInMillis();
if (!fileExists || (milliSeconds - previousEtag.mMilliSeconds >= ADBlockUtils.MILLISECONDS_IN_A_DAY)) {
ADBlockUtils.downloadDatFile(context, fileExists, previousEtag, milliSeconds, fileName, urlString, eTagPrepend, verNumber);
if (0 == oldFileSize || (milliSeconds - previousEtag.mMilliSeconds >= ADBlockUtils.MILLISECONDS_IN_A_DAY)) {
ADBlockUtils.downloadDatFile(context, oldFileSize, previousEtag, milliSeconds, fileName, urlString, eTagPrepend, verNumber);
}

if (downloadOnly) {
return null;
}

return readLocalFile(dataPath.getAbsolutePath());
return readLocalFile(dataPath);
}

public static void downloadDatFile(Context context, boolean fileExist, EtagObject previousEtag, long currentMilliSeconds,
public static void downloadDatFile(Context context, long oldFileSize, EtagObject previousEtag, long currentMilliSeconds,
String fileName, String urlString, String eTagPrepend, String verNumber) {
byte[] buffer = null;
InputStream inputStream = null;
Expand All @@ -117,11 +120,12 @@ public static void downloadDatFile(Context context, boolean fileExist, EtagObjec
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
String etag = connection.getHeaderField("ETag");
int length = connection.getContentLength();
if (null == etag) {
etag = "";
}
boolean downloadFile = true;
if (fileExist && etag.equals(previousEtag.mEtag)) {
if (oldFileSize == length && etag.equals(previousEtag.mEtag)) {
downloadFile = false;
}
previousEtag.mEtag = etag;
Expand All @@ -131,6 +135,7 @@ public static void downloadDatFile(Context context, boolean fileExist, EtagObjec
return;
}
ADBlockUtils.removeOldVersionFiles(context, fileName);

connection.connect();

if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
Expand All @@ -142,11 +147,16 @@ public static void downloadDatFile(Context context, boolean fileExist, EtagObjec
inputStream = connection.getInputStream();
buffer = new byte[ADBlockUtils.BUFFER_TO_READ];
int n = - 1;
int totalReadSize = 0;
while ( (n = inputStream.read(buffer)) != -1)
{
outputStream.write(buffer, 0, n);
totalReadSize += n;
}
outputStream.close();
if (length != totalReadSize) {
ADBlockUtils.removeOldVersionFiles(context, fileName);
}
}
catch (MalformedURLException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public TPFilterParser(Context context) {
}
}

public boolean matchesTrackerJava(String baseHost, String host) {
if (null == mBuffer) {
return false;
}

return matchesTracker(baseHost, host);
}
public String findFirstPartyHostsJava(String baseHost) {
if (null == mBuffer) {
return "";
}

return findFirstPartyHosts(baseHost);
}

public native void init(byte[] data);
public native boolean matchesTracker(String baseHost, String host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public WhiteListCollector(Context context) {

byte[] buffer = null;
if (dataPath.exists()) {
buffer = ADBlockUtils.readLocalFile(dataPath.getAbsolutePath());
buffer = ADBlockUtils.readLocalFile(dataPath);
}
if (null != buffer) {
String[] array = new String(buffer).split(",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ public boolean shouldAdBlockUrl(String baseHost, String urlStr, String filterOpt
return false;
}

return parser.shouldBlock(baseHost, urlStr, filterOption);
return parser.shouldBlockJava(baseHost, urlStr, filterOption);
}

@Override
Expand All @@ -684,9 +684,9 @@ public boolean shouldTrackingProtectionBlockUrl(String baseHost, String host) {
return false;
}

if (tpList.matchesTracker(baseHost, host)) {
if (tpList.matchesTrackerJava(baseHost, host)) {
if (null == mThirdPartyHosts) {
mThirdPartyHosts = tpList.findFirstPartyHosts(baseHost).split(",");
mThirdPartyHosts = tpList.findFirstPartyHostsJava(baseHost).split(",");
}

if (null != mThirdPartyHosts) {
Expand Down

1 comment on commit dcba079

@bbondy
Copy link
Member

@bbondy bbondy commented on dcba079 Mar 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

Please sign in to comment.