Skip to content

Commit 1d54a49

Browse files
committed
Fixed a few buffer overrun bugs due to having assumed a "char" was just 1 byte (it is 2 bytes in .NET), and fixed a loop index bug that caused GetStringAscii to return a string that was just the first character repeated multiple times.
1 parent 634c4fb commit 1d54a49

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

src/Adaptive.Agrona/Concurrent/UnsafeBuffer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ public int GetStringAscii(int index, int length, StringBuilder appendable)
828828
{
829829
for (int i = index + BitUtil.SIZE_OF_INT, limit = index + BitUtil.SIZE_OF_INT + length; i < limit; i++)
830830
{
831-
char c = *(char*)(_pBuffer + index);
831+
char c = (char)*(_pBuffer + i);
832832
appendable.Append(c > (char)127 ? '?' : c);
833833
}
834834

@@ -879,7 +879,7 @@ public int PutStringWithoutLengthAscii(int index, string value)
879879
c = '?';
880880
}
881881

882-
*(char*)(_pBuffer + index + i) = c;
882+
*(_pBuffer + index + i) = (byte)c;
883883
}
884884

885885
return length;
@@ -899,7 +899,7 @@ public int PutStringWithoutLengthAscii(int index, string value, int valueOffset,
899899
c = '?';
900900
}
901901

902-
*(char*)(_pBuffer + index + i) = c;
902+
*(_pBuffer + index + i) = (byte)c;
903903
}
904904

905905
return len;

src/Adaptive.Agrona/ExpandableArrayBuffer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public int GetStringAscii(int index, int length, StringBuilder appendable)
258258
{
259259
for (int i = index + BitUtil.SIZE_OF_INT, limit = index + BitUtil.SIZE_OF_INT + length; i < limit; i++)
260260
{
261-
char c = *(char*)(_pBuffer + index);
261+
char c = (char)*(_pBuffer + i);
262262
appendable.Append(c > (char)127 ? '?' : c);
263263
}
264264

@@ -472,7 +472,7 @@ public int PutStringWithoutLengthAscii(int index, string value)
472472
c = '?';
473473
}
474474

475-
*(char*)(_pBuffer + index + i) = c;
475+
*(_pBuffer + index + i) = (byte)c;
476476
}
477477

478478
return length;
@@ -493,7 +493,7 @@ public int PutStringWithoutLengthAscii(int index, string value, int valueOffset,
493493
c = '?';
494494
}
495495

496-
*(char*)(_pBuffer + index + i) = c;
496+
*(_pBuffer + index + i) = (byte)c;
497497
}
498498

499499
return len;

0 commit comments

Comments
 (0)