Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Contributor
Posts: 6
Registered: ‎11-16-2011
My Device: BB Bold Touch 9900
Accepted Solution

Blackberry OS7 JavaScript shift right (>>) operator issue

Hi,

 

We use JavaScript to create encryption module. It was working without issue until recently it was not working in Blackberry OS 7 (tested on Blackberry Bold Touch 9900). It was not throwing error but was generating encryption result that was unable to be decrypted in the server side. We have tested with old Blackberry browser (BB Bold 9700 running OS5) & it was working. We try to install BOLT browser in BB OS 7 device & the encryption also working there. So the issue is isolated to default browser in BB OS 7.

 

After debugging session by comparing the result from other browser to BB OS 7 browser, we found that the result is different because BB OS 7 browser sometime will generate different result for SHIFT RIGHT (>>) operation compared to other browser (tested in IE8 & Chrome). 

 

Based on the snippet below:

 

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

 

When passed with the following value x = -271733879 & y = -2160220835

Both browsers generate same value for lsw variable = 86246

Both browsers generate same value for (x >> 16) = -4147

BB OS 7 Browsers generate different value for (yy >> 16). BB = -32768 but other browser = 32573 

Both browser generate same value for (lsw >> 16) = 1

Because of this both browser generate different value for msw variable & subsequently the return value.

 

Is this a bug in default browser for BB OS 7? If yes, any possible workaround for this? NOTE: the encryption logic use shift right in many place.

 

Thanks,

Willy

 

Highlighted
Retired
Posts: 1,561
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Blackberry OS7 JavaScript shift right (>>) operator issue

Hi Willy,

 

I've done a quick test with the sample you provided . Testing on my 9900 with 7.1, the result for y >> 16 was 32573; this was the same result as in my Firefox browser.

 

I'm loading up some 7.0 devices to see the behaviour there and was hoping you could confirm the exact OS version you are running (obtainable via: Options > Device > About Device Versions.)

 

If this is reproducible on that same 7.0 version, it very well looks to be an OS/Browser issue. The fact that I see the correct result on 7.1 makes me hopeful that this will have already been addressed in newer versions of 7.0 as well, but we'll need to do a few tests to double-check.

 

Erik Oros

BlackBerry Development Advisor


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Retired
Posts: 1,561
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Blackberry OS7 JavaScript shift right (>>) operator issue

I confirmed also on a 9810 and 9930 running 7.0 and received consistent (good) results on both devices. Specifically, y >> 16 resulted in 32573 on both devices.

 

I have a hunch that this will only be an issue on the 9900 up to a certain OS version after which it will be fixed. If you can confirm your specific OS version I see if I am able to reproduce the issue.

 

Erik Oros

BlackBerry Development Advisor


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
New Contributor
Posts: 6
Registered: ‎11-16-2011
My Device: BB Bold Touch 9900

Re: Blackberry OS7 JavaScript shift right (>>) operator issue

Hi Erik,

 

Thanks for the reply. My BB version (from About window) are:

Blackberry 9900 (3G, WiFi)

7.0 Bundle 1346 (v7.0.0.261, Platform 5.0.0.464)

3G Bands 1,2,5.6

Cryptographic Kernal v3.8.7.0

 

Retired
Posts: 1,561
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Blackberry OS7 JavaScript shift right (>>) operator issue

Hello again,

 

I didn't have a 9900 running 7.0 available, however I did test on a 9930 running 7.0, Bundle 1346 and the value for y >> 16 resulted in -32768.

 

So it does appear that this is an OS issue on Bundle 1346, however has been resolved on a subsequent 7.0 OS.

 

Depending on your carrier, a newer version than Bundle 1346 may be available. The best recommendation at this point would be to grab the newest release available at the following URL and test with that:

http://us.blackberry.com/support/downloads/download_sites.jsp

 

For instance, Verizon has Bundle 1739 available; but this will be dependant on the carrier.

 

I did test Bundle 1739 myself and still saw -32768 returned for y >> 16, so there is the possibility that the fixed OS version has not been accepted by carriers yet.

 

As a workaround, I was able to write:

var yrshift = y >> 16;

 

As the following instead:

 var yrshift = (y & 0xFFFF0000) / 65536;

 

In essence this does the same thing; takes the top 16-bits and shifts 16 bits to the right (2^16 = 65536.) With this approach, I receieved the correct value 32573 on Bundle 1739 and believe this would also work on the earlier versions.

 

If a new OS does not resolve your issue, or you require this functionality on the identified broken OS versions, the above should hopefully meet your needs.

 

Erik Oros

BlackBerry Development Advisor


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
New Contributor
Posts: 6
Registered: ‎11-16-2011
My Device: BB Bold Touch 9900

Re: Blackberry OS7 JavaScript shift right (>>) operator issue

Hi Erik,

Thanks for confirming the behaviour & providing the work around.

Do you know whether the bug only affecting SHIFT RIGHT 16 bit ( >> 16) only or could affect other no of bit ? Because we use SHIFT RIGHT in many other part of the code & we may need to change other lines of code as well (I think if affect other no of bit then the replacement logic should be y & !(2^no_of_bit) / (2^no_of_bit)). But doing the replacement logic I suspect will impact performance as SHIFT LEFT / RIGHT is much faster than the above operation. As much as possible, we will try to use the workaround to minimize impact to the customer. However if the changes too big / performance suffer greatly, we may advise customer to wait for the fixed OS version. Do you have the OS bundle version that already fixed the version (which you have tested good previously on your 9810 & 9930)?

Thanks,
Willy
New Contributor
Posts: 6
Registered: ‎11-16-2011
My Device: BB Bold Touch 9900

Re: Blackberry OS7 JavaScript shift right (>>) operator issue

Hi Erik,

 

I think the workaround works for >>> (Shift Right Zero Fill) operator but not for >> (Shift Right with Sign) operator. For >> operator need to add "| 0xFFFF0000" to fill as per the sign value if value in decimal < 0.

 

However I just notice that -2160220835 is not a valid 32-bit integer value. Most likely this is the reason why the calculation was orignally wrong.

 

Thanks,
Willy

New Contributor
Posts: 6
Registered: ‎11-16-2011
My Device: BB Bold Touch 9900

Re: Blackberry OS7 JavaScript shift right (>>) operator issue

Hi Erik,

 

I created this workaround based on the assumption that the error only occured if we use >> on a value that is > 32 bit integer range.  

 

<script>
function ShiftRight(x, noOfBits) 
{
	return (x >> noOfBits);
}
var powerOfTwo = [ 
	0x00000001, 0x00000002, 0x00000004, 0x00000008,
	0x00000010, 0x00000020, 0x00000040, 0x00000080,
	0x00000100, 0x00000200, 0x00000400, 0x00000800,
	0x00001000, 0x00002000, 0x00004000, 0x00008000,
	0x00010000, 0x00020000, 0x00040000, 0x00080000,
	0x00100000, 0x00200000, 0x00400000, 0x00800000,
	0x01000000, 0x02000000, 0x04000000, 0x08000000,
	0x10000000, 0x20000000, 0x40000000, 0x80000000
]
var bitMaskArray = null;
function ShiftRight2(n, noOfBits) //NOTE: int must be 32-bit so max noOfBits = 32
{
	if (bitMaskArray == null) 
	{
		bitMaskArray = new Array(32);
		for(i = 0; i < 32; i++) 
		{
			if (i > 0) {
				bitMaskArray[i] = bitMaskArray[i-1] + powerOfTwo[i];
			}
			else {
				bitMaskArray[i] = powerOfTwo[i];
			}
		}
	}
	if (n >= -2147483648 && n <= 2147483647) //if within normal 32-bit range just use the same logic (FASTER & MORE RELIABLE)
	{
		return (n >> noOfBits);
	}
	else 
	{
		if (noOfBits < 32) {
			return (
				((n & (~bitMaskArray[noOfBits-1])) / powerOfTwo[noOfBits]) //as >> n basically can be read as faster way to do "1/(2^noOfBits)" that's why the clean up result; (n & (~bitMaskArray[noOfBits-1]) will remove the low digit value so that the divide result will always be integer value
				| ((n&powerOfTwo[31])==(powerOfTwo[31])?(~bitMaskArray[noOfBits-1]):0) //if the sign digit is 1 then fill the start of the bit with 1. 2^31 = sign digit
			); 
		}
		else {
			return 0;
		}
	}
}
var shiftRightFunc = ((-2160220835 >> 16) == 32573)?ShiftRight:ShiftRight2; //detect which function to use
</script>

 

I will test this logic & see whether still got problem with BB OS7 earlier build. Once the good OS7 build is fixed, by right it should be reverted to the old function.

New Contributor
Posts: 6
Registered: ‎11-16-2011
My Device: BB Bold Touch 9900

Re: Blackberry OS7 JavaScript shift right (>>) operator issue

Hi Erik,

Seems like the solution fixed the issues on BB OS7 early build. We are doing full regression testing now to ensure that the fix really work without causing other issues. Thanks for the work around script.

Regards,
Willy
New Contributor
Posts: 2
Registered: ‎12-14-2011
My Device: Bold 9900
My Carrier: None

Re: Blackberry OS7 JavaScript shift right (>>) operator issue

Hi Erik, 

 

Currently I am facing the similar problem. And after doing some investigation we found out that the result of the shift right operator was different when performing shift right operator (>>) by using variable.

If passing the value directly the result will be the same with other browser.

 

Following is the code snipped that used for testing:

var y = -2160220835;

alert('A: ' + (y>>16));

alert('B: ' + (-2160220835>>16));

 

the result:

A: -32768

B: 32573 (same with other browser)

 

Is there any suggestion to fix this issue and I wonder would it be possible to know whether the behavior will impact the other operation or not?

 

Thank you and regards, 

Teguh