Skip to content
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

Accepts empty string as valid semver (i.e. treats major component as optional) #29

Open
JC3 opened this issue Dec 10, 2021 · 0 comments

Comments

@JC3
Copy link

JC3 commented Dec 10, 2021

I don't think it's generally a problem to be loose with semver parsing, but I wonder if semver_parse("", ...) should fail instead of the current behavior of yielding 0.0.0?

A related side effect of this is "-1.2.3" is parsed as 0.0.0 with prerelease "1.2.3", which probably wasn't the intent of that string. In general, anything that forms a valid prerelease and/or metadata suffix is accepted as a valid semver in itself.

And other strangeness:

void testparse (const char *str) {

    semver_t v = {};

    printf("%-10s: ", str);
    if (semver_parse(str, &v))
        printf("error");
    else
        printf("major=%d  minor=%d  patch=%d  pre=%-8s  meta=%s", 
               v.major, v.minor, v.patch, v.prerelease, v.metadata);
    printf("\n");

    semver_free(&v);

}

int main () {

    testparse("");
    testparse("-1.2.3");

    testparse("-");
    testparse("+");
    testparse("-+");
    testparse("-3.14159");
    testparse("+10");

    testparse("fail");

}

Outputs:

          : major=0  minor=0  patch=0  pre=(null)    meta=(null)
-1.2.3    : major=0  minor=0  patch=0  pre=1.2.3     meta=(null)
-         : major=0  minor=0  patch=0  pre=          meta=(null)
+         : major=0  minor=0  patch=0  pre=(null)    meta=
-+        : major=0  minor=0  patch=0  pre=          meta=
-3.14159  : major=0  minor=0  patch=0  pre=3.14159   meta=(null)
+10       : major=0  minor=0  patch=0  pre=(null)    meta=10
fail      : error

All of those seem like they should, at least philosophically, result in parse failures. 🤷‍♂️

Having the parser verify that a major component is present would cover all those cases as well as the empty string case.

I mean, there's loose, and then there's loose... 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant