Skip to content

Commit bb19cbe

Browse files
committed
Remove C# unsafe code in ByteBuffer
The "unsafe" code used in ByteBuffer to read and write doubles and float was causing problem with Android mono implementation. See: https://forums.zeroc.com/discussion/46631/build-ice-3-7-1-with-option-managed-yes-on-macos-or-windows
1 parent 7c92d14 commit bb19cbe

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

csharp/src/Ice/ByteBuffer.cs

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,13 @@ public short getShort()
323323
return v;
324324
}
325325

326-
unsafe
327326
public short getShort(int pos)
328327
{
329328
checkUnderflow(pos, 2);
330329
if(NO._o == _order)
331330
{
332-
fixed(byte* p = &_bytes[pos])
333-
{
334-
_valBytes.shortVal = *((short*)p);
335-
}
331+
_valBytes.b0 = _bytes[pos];
332+
_valBytes.b1 = _bytes[pos + 1];
336333
}
337334
else
338335
{
@@ -363,17 +360,14 @@ public void getShortSeq(short[] seq)
363360
_position += len;
364361
}
365362

366-
unsafe
367363
public ByteBuffer putShort(short val)
368364
{
369365
checkOverflow(2);
370366
_valBytes.shortVal = val;
371367
if(NO._o == _order)
372368
{
373-
fixed(byte* p = &_bytes[_position])
374-
{
375-
*((short*)p) = _valBytes.shortVal;
376-
}
369+
_bytes[_position] = _valBytes.b0;
370+
_bytes[_position + 1] = _valBytes.b1;
377371
}
378372
else
379373
{
@@ -406,16 +400,15 @@ public ByteBuffer putShortSeq(short[] seq)
406400
return this;
407401
}
408402

409-
unsafe
410403
public int getInt()
411404
{
412405
checkUnderflow(4);
413406
if(NO._o == _order)
414407
{
415-
fixed(byte* p = &_bytes[_position])
416-
{
417-
_valBytes.intVal = *((int*)p);
418-
}
408+
_valBytes.b0 = _bytes[_position];
409+
_valBytes.b1 = _bytes[_position + 1];
410+
_valBytes.b2 = _bytes[_position + 2];
411+
_valBytes.b3 = _bytes[_position + 3];
419412
}
420413
else
421414
{
@@ -458,7 +451,6 @@ public ByteBuffer putInt(int val)
458451
return this;
459452
}
460453

461-
unsafe
462454
public ByteBuffer putInt(int pos, int val)
463455
{
464456
if(pos < 0)
@@ -472,10 +464,10 @@ public ByteBuffer putInt(int pos, int val)
472464
_valBytes.intVal = val;
473465
if(NO._o == _order)
474466
{
475-
fixed(byte* p = &_bytes[pos])
476-
{
477-
*((int*)p) = _valBytes.intVal;
478-
}
467+
_bytes[pos] = _valBytes.b0;
468+
_bytes[pos + 1] = _valBytes.b1;
469+
_bytes[pos + 2] = _valBytes.b2;
470+
_bytes[pos + 3] = _valBytes.b3;
479471
}
480472
else
481473
{
@@ -518,16 +510,19 @@ public long getLong()
518510
return v;
519511
}
520512

521-
unsafe
522513
public long getLong(int pos)
523514
{
524515
checkUnderflow(pos, 8);
525516
if(NO._o == _order)
526517
{
527-
fixed(byte* p = &_bytes[pos])
528-
{
529-
_valBytes.longVal = *((long*)p);
530-
}
518+
_valBytes.b0 = _bytes[pos];
519+
_valBytes.b1 = _bytes[pos + 1];
520+
_valBytes.b2 = _bytes[pos + 2];
521+
_valBytes.b3 = _bytes[pos + 3];
522+
_valBytes.b4 = _bytes[pos + 4];
523+
_valBytes.b5 = _bytes[pos + 5];
524+
_valBytes.b6 = _bytes[pos + 6];
525+
_valBytes.b7 = _bytes[pos + 7];
531526
}
532527
else
533528
{
@@ -570,17 +565,20 @@ public void getLongSeq(long[] seq)
570565
_position += len;
571566
}
572567

573-
unsafe
574568
public ByteBuffer putLong(long val)
575569
{
576570
checkOverflow(8);
577571
_valBytes.longVal = val;
578572
if(NO._o == _order)
579573
{
580-
fixed(byte* p = &_bytes[_position])
581-
{
582-
*((long*)p) = _valBytes.longVal;
583-
}
574+
_bytes[_position] = _valBytes.b0;
575+
_bytes[_position + 1] = _valBytes.b1;
576+
_bytes[_position + 2] = _valBytes.b2;
577+
_bytes[_position + 3] = _valBytes.b3;
578+
_bytes[_position + 4] = _valBytes.b4;
579+
_bytes[_position + 5] = _valBytes.b5;
580+
_bytes[_position + 6] = _valBytes.b6;
581+
_bytes[_position + 7] = _valBytes.b7;
584582
}
585583
else
586584
{
@@ -625,16 +623,15 @@ public ByteBuffer putLongSeq(long[] seq)
625623
return this;
626624
}
627625

628-
unsafe
629626
public float getFloat()
630627
{
631628
checkUnderflow(4);
632629
if(NO._o == _order)
633630
{
634-
fixed(byte* p = &_bytes[_position])
635-
{
636-
_valBytes.floatVal = *((float*)p);
637-
}
631+
_valBytes.b0 = _bytes[_position];
632+
_valBytes.b1 = _bytes[_position + 1];
633+
_valBytes.b2 = _bytes[_position + 2];
634+
_valBytes.b3 = _bytes[_position + 3];
638635
}
639636
else
640637
{
@@ -670,17 +667,16 @@ public void getFloatSeq(float[] seq)
670667
_position += len;
671668
}
672669

673-
unsafe
674670
public ByteBuffer putFloat(float val)
675671
{
676672
checkOverflow(4);
677673
_valBytes.floatVal = val;
678674
if(NO._o == _order)
679675
{
680-
fixed(byte* p = &_bytes[_position])
681-
{
682-
*((float*)p) = _valBytes.floatVal;
683-
}
676+
_bytes[_position] = _valBytes.b0;
677+
_bytes[_position + 1] = _valBytes.b1;
678+
_bytes[_position + 2] = _valBytes.b2;
679+
_bytes[_position + 3] = _valBytes.b3;
684680
}
685681
else
686682
{
@@ -717,16 +713,19 @@ public ByteBuffer putFloatSeq(float[] seq)
717713
return this;
718714
}
719715

720-
unsafe
721716
public double getDouble()
722717
{
723718
checkUnderflow(8);
724719
if(NO._o == _order)
725720
{
726-
fixed(byte* p = &_bytes[_position])
727-
{
728-
_valBytes.doubleVal = *((double*)p);
729-
}
721+
_valBytes.b0 = _bytes[_position];
722+
_valBytes.b1 = _bytes[_position + 1];
723+
_valBytes.b2 = _bytes[_position + 2];
724+
_valBytes.b3 = _bytes[_position + 3];
725+
_valBytes.b4 = _bytes[_position + 4];
726+
_valBytes.b5 = _bytes[_position + 5];
727+
_valBytes.b6 = _bytes[_position + 6];
728+
_valBytes.b7 = _bytes[_position + 7];
730729
}
731730
else
732731
{
@@ -770,17 +769,20 @@ public void getDoubleSeq(double[] seq)
770769
_position += len;
771770
}
772771

773-
unsafe
774772
public ByteBuffer putDouble(double val)
775773
{
776774
checkOverflow(8);
777775
_valBytes.doubleVal = val;
778776
if(NO._o == _order)
779777
{
780-
fixed(byte* p = &_bytes[_position])
781-
{
782-
*((double*)p) = _valBytes.doubleVal;
783-
}
778+
_bytes[_position] = _valBytes.b0;
779+
_bytes[_position + 1] = _valBytes.b1;
780+
_bytes[_position + 2] = _valBytes.b2;
781+
_bytes[_position + 3] = _valBytes.b3;
782+
_bytes[_position + 4] = _valBytes.b4;
783+
_bytes[_position + 5] = _valBytes.b5;
784+
_bytes[_position + 6] = _valBytes.b6;
785+
_bytes[_position + 7] = _valBytes.b7;
784786
}
785787
else
786788
{

0 commit comments

Comments
 (0)