godot: Some UID string values are incorrectly re-encoded

Tested versions

Reproducible in 4.3, master - anything that has UID support - see below for code.

System information

Windows 10, but platform is unimportant for this issue

Issue description

Some examples:

  1. Input: "uid://a" As ID: 0 (via ResourceUID.text_to_id("uid://a")) Back to text: "uid://" (via ResourceUID.id_to_text(0)) Note how the output is an incorrect/invalid uid that is also different to the input

  2. Input: "uid://abc" As ID: 36 Back to text: "uid://bc" Note how the output is different to the input

  3. Input: "uid://abcdefabcdefabcdefabcdef" As ID: 5093833392511097721 Back to text: "uid://cetqxrw13eh3f" Note how the output is different to the input

I suggest we try to make the encoding scheme bijective - a numeric value should correspond to a single uid://-encoded string, and vice-versa. Anything that’s not in that space should encode to invalid.

I also noticed there are no tests for this, so I am opening a PR with some tests.

There’s also some other known issues with these (‘9’ and ‘z’ not being included in the IDs) but I am opening this because I reimplemented uid support in another language and was surprised to find these issues.

Thoughts?

Steps to reproduce

Run the gdscript below 😃

Minimal reproduction project (MRP)

func test(res):
  var id1 = ResourceUID.text_to_id(res)
  var res2 = ResourceUID.id_to_text(id1)
  print(res + " is " + str(id1) + " and back to str is " + res2)
  
test("uid://a")
test("uid://abc")
test("uid://abcdefabcdefabcdefabcdef")

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (18 by maintainers)

Most upvoted comments

Also this “uid://abcdefabcdefabcdefabcdef” is an invalid uid, as according to comments in resource_uid.cpp the maximum length of string uid is 19 characters. “abcdefabcdefabcdefabcdef” is 24 characters long.

BTW looking at its usage max_uuid_number_length = 19 is incorrect, it should be 13.

0x7FFFFFFFFFFFFFFF -> uid://d4n4ub6itg400

So the max length of the number part is 13 (d4n4ub6itg400), it would be +6 when accounting for uid:// (or +7 with terminating null). max_uuid_number_length is used only for creating temp buffer for converting the number, so it can be lowered to 13: https://github.com/godotengine/godot/blob/2582793d408ade0b6ed42f913ae33e7da5fb9184/core/io/resource_uid.cpp#L50-L87

The uid://a case is due to a bug I’d say, I’ll write a fix soon, there’s no reason IMO to preserve that, unlike the re-encoding by changing the format as mentioned in #83843

uid:// is already being converted to 0 so everything should work fine with it I think (aka probably not a real bug), but indeed would make more sense so 0 would produce uid://a[^1]. As mentioned all of uid://, uid://a, uid://aa, uid://aaa, (…) already produce 0 so changing this shouldn’t be anyhow breaking. 🤔

[^1]: It would make even more sense if 0 would produce uid://0 but for whatever reason the numerical system used in the implementation firstly uses letters instead of numbers… 🙃

For completeness when it comes to a one-to-one mapping we also have that ResourceUID.id_to_text(-1) == ResourceUID.id_to_text(-2) == "uid://<invalid>", though again you would never get a UID of -2 by normal means

Good catch will fix the length constant as well

The uid://a case is due to a bug I’d say, I’ll write a fix soon, there’s no reason IMO to preserve that, unlike the re-encoding by changing the format as mentioned in #83843

However uid://abc is not a bug, it’s simply the same as, for example, "012" -> 12 -> "12", leading a is equivalent to leading zero, we could add a warning for invalid input but technically uid://ab is invalid, or at least not proper

Your tests start from strings, e.g. “abc”, but the actual uids are integers. So I don’t think it is a problem that some random string1 -> int -> string2 conversions produce situations where string1 != string2. That just means that the string1 is not a valid string representation of any integer uid. The important conversion is int1 -> string -> int2. int1 should always be equal to int2.

Also this “uid://abcdefabcdefabcdefabcdef” is an invalid uid, as according to comments in resource_uid.cpp the maximum length of string uid is 19 characters. “abcdefabcdefabcdefabcdef” is 24 characters long.

According to this https://github.com/godotengine/godot/issues/83843, the int <-> string conversion will not be changed because of backwards compatibility.

I briefly tested what happens when you have a uid 0. I had a scene and an attachted script. I changed script’s uid to uid:// both in the scene file and script’s .uid file. It somewhat worked without any error messages, but at some point a new uid was created for the script to the scene file, but the script’s .uid file was not changed. So I would say that uid 0 does not work correctly.

Have you tried exactly the same actions/changes using a non-zero UID though? I mean just because it bugged for 0 doesn’t mean it’s a bug specific for 0, might be some more general issue/bug occuring for any UID. I’d say changing UIDs manually is rather questionable, I’d expect that I could break something by doing so, like possibly making some cache be in an invalid state or something like that. Of course would be nice if it would be properly detected and gracefully handled.

Anyway, that sounds like potentially a separate issue. If you can reliably reproduce it then please open a new issue for it with all the details provided there (unless it’s already reported; after quick search I found e.g. #99779 which is quite different but of similar nature (UIDs vs user manual tinkering)).

Having a != 0 check when creating uids has a non measurable performance cost, at least compared how much is the total time the function takes.

Yeah I wouldn’t bother about it performance-wise, it should be fine. Still, I don’t see a proper justification for disallowing 0.

I don’t think “something doesn’t work for 0 so let’s disallow 0” is a correct approach/reasoning. Let’s firstly find out what exactly fails and why, and then decide what should be done about it.

🎉 Folks, it’s 2025 here now - happy new year! 🎉

uid:// is already being converted to 0 so everything should work fine with it I think (aka probably not a real bug), but indeed would make more sense so 0 would produce uid://a1. As mentioned all of uid://, uid://a, uid://aa, uid://aaa, (…) already produce 0 so changing this shouldn’t be anyhow breaking. 🤔

I briefly tested what happens when you have a uid 0. I had a scene and an attachted script. I changed script’s uid to uid:// both in the scene file and script’s .uid file. It somewhat worked without any error messages, but at some point a new uid was created for the script to the scene file, but the script’s .uid file was not changed. So I would say that uid 0 does not work correctly.

Having a != 0 check when creating uids has a non measurable performance cost, at least compared how much is the total time the function takes.

I don’t think any advanced checking is needed as you would only encounter them if you intentionally make your own invalid ones, and you shouldn’t really just create random uid strings, there’s no use case for that I’d say

We could add a check but it’d just be a waste of performance IMO

But I will write a fix for the uid://a case

Personally, I suggest that once it’s possible, there should be one and only one valid string for each valid ID

There is, you will never have it output leading a, but I don’t see any reason to complicate the process and add checks for “overlong” UID strings, I don’t think it makes sense to compare them as strings anyway, they should be operated on as integers

The uid://a case is due to a bug I’d say, I’ll write a fix soon, there’s no reason IMO to preserve that, unlike the re-encoding by changing the format as mentioned in #83843

I also noticed that it is possible that ResourceUID::create_id() can create an uid with value 0. It’s string representation is "uid://" which will cause problems. I think this is fixable by changing the uid creation so that 0 uid will not be created. The risk of creating 0 is very unlikely but I don’t see any reason why that risk would be worth taking.