Projects tigase _server server-core Issues #1197
Infinite loop while cutting body of encrypted push notification to fit the push notifications limit (#1197)
Closed
Andrzej Wójcik (Tigase) opened 4 years ago
Affected versions
tigase-issue #8.1.0

Infinite loop while cutting body of encrypted push notification to fit the push notifications limit

Andrzej Wójcik (Tigase) commented 4 years ago

while block had invalid conditions causing in rare cases infinite loop but also was not cutting string properly. I've changed code to take into considerations emoji, etc.

Andrzej Wójcik (Tigase) commented 4 years ago

@wojtek The same error is in 8.1.0 so it may be good to consider backporting this fix for a next bugfix release.

wojciech.kapcia@tigase.net commented 4 years ago

As per off-YT conversation: please add relevant test cases for the issue and fix (so someone wouldn't apply other "fix" that would actually break something)

wojciech.kapcia@tigase.net commented 4 years ago

According to the test case that you provided we could simplify trimBodyToSize() to:

public static String trimBodyToSize(long limit, String body) {
	CharsetEncoder enc = StandardCharsets.UTF_8.newEncoder();
	ByteBuffer bb = ByteBuffer.allocate((int) limit);
	CharBuffer cb = CharBuffer.wrap(body);
	CoderResult r = enc.encode(cb, bb, true);
	return r.isOverflow() ? cb.flip().toString() : body;
}

But it seems you were not OK with it. The idea of having a unit test was to avoid erroneous "improvement". Please provide proper test that indicates what current solution is the only proper one (and above yields incorrect result)

Btw. I think limit parameter could be of int type here?

Andrzej Wójcik (Tigase) commented 4 years ago

I've added more tests to check chars with different code ranges from UTF8 specification and confirmed that your code works (in some cases even better). So I've replaced implementation and made a few changes, adjustments and comments, so I suppose that it is complete now.

issue 1 of 1
Type
Bug
Priority
Normal
Assignee
Version
tigase-server-8.2.0
Spent time
5h 30m
Issue Votes (0)
Watchers (0)
Reference
tigase/_server/server-core#1197
Please wait...
Page is in error, reload to recover