Skip to content

Fix "invalid lib version" crash #7920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 28, 2018
Merged
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 @@ -32,6 +32,7 @@
import com.github.zafarkhaja.semver.Version;

import java.io.File;
import java.util.Optional;

public abstract class DownloadableContribution {

Expand Down Expand Up @@ -66,10 +67,10 @@ public void setDownloadedFile(File downloadedFile) {
}

public String getParsedVersion() {
Version version = VersionHelper.valueOf(getVersion());
if (version == null) {
return null;
Optional<Version> version = VersionHelper.valueOf(getVersion());
if (version.isPresent()) {
return version.get().toString();
}
return version.toString();
return null;
}
}
48 changes: 19 additions & 29 deletions arduino-core/src/cc/arduino/contributions/VersionComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,32 @@
import cc.arduino.contributions.libraries.ContributedLibrary;

import java.util.Comparator;
import java.util.Optional;

public class VersionComparator implements Comparator<String> {

@Override
public int compare(String a, String b) {
// null is always less than any other value
if (a == null && b == null)
return 0;
if (a == null)
return -1;
if (b == null)
return 1;

Version versionA = VersionHelper.valueOf(a);
Version versionB = VersionHelper.valueOf(b);

return versionA.compareTo(versionB);
}

public static boolean greaterThan(String a, String b) {
// null is always less than any other value
if (a == null && b == null) {
return false;
public static int compareTo(String a, String b) {
Optional<Version> versionA = VersionHelper.valueOf(a);
Optional<Version> versionB = VersionHelper.valueOf(b);
if (versionA.isPresent() && versionB.isPresent()) {
return versionA.get().compareTo(versionB.get());
}
if (a == null) {
return false;
if (versionA.isPresent()) {
return 1;
}
if (b == null) {
return true;
if (versionB.isPresent()) {
return -1;
}
return 0;
}

Version versionA = VersionHelper.valueOf(a);
Version versionB = VersionHelper.valueOf(b);
@Override
public int compare(String a, String b) {
return compareTo(a, b);
}

return versionA.greaterThan(versionB);
public static boolean greaterThan(String a, String b) {
return compareTo(a, b) > 0;
}

public static String max(String a, String b) {
Expand All @@ -79,8 +70,7 @@ public static ContributedLibrary max(ContributedLibrary a, ContributedLibrary b)
return greaterThan(a, b) ? a : b;
}

public static boolean greaterThan(ContributedLibrary a,
ContributedLibrary b) {
public static boolean greaterThan(ContributedLibrary a, ContributedLibrary b) {
return greaterThan(a.getParsedVersion(), b.getParsedVersion());
}
}
13 changes: 7 additions & 6 deletions arduino-core/src/cc/arduino/contributions/VersionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@

package cc.arduino.contributions;

import java.util.Optional;

import com.github.zafarkhaja.semver.Version;

public class VersionHelper {

public static Version valueOf(String ver) {
public static Optional<Version> valueOf(String ver) {
if (ver == null) {
return null;
return Optional.empty();
}
try {
// Allow x.y-something, assuming x.y.0-something
Expand All @@ -49,18 +51,17 @@ public static Version valueOf(String ver) {
}
String[] parts = version.split("\\.");
if (parts.length >= 3) {
return Version.valueOf(ver);
return Optional.of(Version.valueOf(ver));
}
if (parts.length == 2) {
version += ".0";
}
if (parts.length == 1) {
version += ".0.0";
}
return Version.valueOf(version + extra);
return Optional.of(Version.valueOf(version + extra));
} catch (Exception e) {
System.err.println("Invalid version found: " + ver);
return null;
return Optional.empty();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.List;
import java.util.Optional;

import static processing.app.I18n.format;
import static processing.app.I18n.tr;

public class LibrariesIndexer {
Expand Down Expand Up @@ -203,7 +204,7 @@ private void scanLibrary(UserLibraryFolder folderDesc) throws IOException {
LegacyUserLibrary lib = LegacyUserLibrary.create(folderDesc);
String[] headers = BaseNoGui.headerListFromIncludePath(lib.getSrcFolder());
if (headers.length == 0) {
throw new IOException(lib.getSrcFolder().getAbsolutePath());
throw new IOException(format(tr("no headers files (.h) found in {0}"), lib.getSrcFolder()));
}
addToInstalledLibraries(lib);
return;
Expand All @@ -213,7 +214,7 @@ private void scanLibrary(UserLibraryFolder folderDesc) throws IOException {
UserLibrary lib = UserLibrary.create(folderDesc);
String[] headers = BaseNoGui.headerListFromIncludePath(lib.getSrcFolder());
if (headers.length == 0) {
throw new IOException(lib.getSrcFolder().getAbsolutePath());
throw new IOException(format(tr("no headers files (.h) found in {0}"), lib.getSrcFolder()));
}
addToInstalledLibraries(lib);

Expand Down
22 changes: 15 additions & 7 deletions arduino-core/src/processing/app/packages/UserLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@
*/
package processing.app.packages;

import cc.arduino.Constants;
import cc.arduino.contributions.VersionHelper;
import cc.arduino.contributions.libraries.ContributedLibraryReference;
import processing.app.helpers.PreferencesMap;
import processing.app.packages.UserLibraryFolder.Location;
import static processing.app.I18n.format;
import static processing.app.I18n.tr;

import java.io.File;
import java.io.IOException;
Expand All @@ -41,9 +38,16 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import com.github.zafarkhaja.semver.Version;

import cc.arduino.Constants;
import cc.arduino.contributions.VersionHelper;
import cc.arduino.contributions.libraries.ContributedLibraryReference;
import processing.app.helpers.PreferencesMap;
import processing.app.packages.UserLibraryFolder.Location;

public class UserLibrary {

private String name;
Expand Down Expand Up @@ -148,12 +152,16 @@ public static UserLibrary create(UserLibraryFolder libFolderDesc) throws IOExcep
}

String declaredVersion = properties.get("version").trim();
Version version = VersionHelper.valueOf(declaredVersion);
Optional<Version> version = VersionHelper.valueOf(declaredVersion);
if (!version.isPresent()) {
System.out.println(
format(tr("Invalid version '{0}' for library in: {1}"), declaredVersion, libFolder.getAbsolutePath()));
}

UserLibrary res = new UserLibrary();
res.installedFolder = libFolder;
res.name = properties.get("name").trim();
res.version = version.toString();
res.version = version.isPresent() ? version.get().toString() : declaredVersion;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could using "declaredVersion" have unintended consequences? As I understand this, it would allow libraries to put any arbitrary string into Arduino's data structures. Is everything else in the Arduino code able to handle any arbitrary text, when it's all been designed around the until-now strictly enforced major.minor.patch format?

Copy link
Member Author

Choose a reason for hiding this comment

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

The version field for user libraries has always been a String and the format has never been forced to be semver compliant.

To compare version "strings" we use the VersionComparator class that compares two different version "strings", say A and B, using the following alogrithm:

  1. parse A and B as semver
  2. if both A and B are valid semver, compare them using semver rules
  3. if A is valid semver but not B, then A is the greatest
  4. if B is valid semver but not A, then B is the greatest
  5. if both A and B are not valid semver, consider them equals

This logic allows libraires that are not semver compliant to soft transition to semver (since a valid semver version is always greater).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, sounds like you've thought this through quite carefully. I was thinking simpler less-is-more in #7922, after having a user reports 1.8.6 unable to start up.

res.author = properties.get("author").trim();
res.maintainer = properties.get("maintainer").trim();
res.sentence = properties.get("sentence").trim();
Expand Down