Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

off-by-one in tbprobe.c #205

Open
patricklauer opened this issue Oct 31, 2021 · 0 comments
Open

off-by-one in tbprobe.c #205

patricklauer opened this issue Oct 31, 2021 · 0 comments

Comments

@patricklauer
Copy link

./cfish bench 16 1 15 default depth nnue > /dev/null
=================================================================
==12099==ERROR: AddressSanitizer: global-buffer-overflow on address 0x55f3d13e3b60 at pc 0x55f3d13143cc bp 0x7ffc916dac00 sp 0x7ffc916dabf0
READ of size 8 at 0x55f3d13e3b60 thread T0
    #0 0x55f3d13143cb in init_indices /home/me/chess/Cfish/src/tbprobe.c:685
    #1 0x55f3d13143cb in TB_init /home/me/chess/Cfish/src/tbprobe.c:338
    #2 0x55f3d132a4e0 in on_tb_path /home/me/chess/Cfish/src/ucioption.c:71
    #3 0x55f3d1329dd6 in options_init /home/me/chess/Cfish/src/ucioption.c:192
    #4 0x55f3d12b8dfa in main /home/me/chess/Cfish/src/main.c:46
    #5 0x7f481d6007fc in __libc_start_main (/lib64/libc.so.6+0x237fc)
    #6 0x55f3d12b93a9 in _start (/home/me/chess/Cfish/src/cfish+0x293a9)

0x55f3d13e3b60 is located 32 bytes to the left of global variable 'Binomial' defined in 'tbprobe.c:671:15' (0x55f3d13e3b80) of size 3584
0x55f3d13e3b60 is located 0 bytes to the right of global variable 'PawnIdx' defined in 'tbprobe.c:672:15' (0x55f3d13e3260) of size 2304
SUMMARY: AddressSanitizer: global-buffer-overflow /home/me/chess/Cfish/src/tbprobe.c:685 in init_indices
Shadow bytes around the buggy address:
[...]

This is due to an index off-by-one, and is fixed with

diff --git a/src/tbprobe.c b/src/tbprobe.c
index 260a255..7625c86 100644
--- a/src/tbprobe.c
+++ b/src/tbprobe.c
@@ -680,9 +680,11 @@ static void init_indices(void)
   // Binomial[k][n] = Bin(n, k)
   for (j = 0; j < 64; j++)
     Binomial[0][j] = 1;
-  for (i = 0; i < 7; i++)
-    for (j = 1; j < 64; j++)
+  for (i = 1; i < 7; i++) {
+    for (j = 1; j < 64; j++) {
       Binomial[i][j] = Binomial[i - 1][j - 1] + Binomial[i][j - 1];
+    }
+  }

   for (i = 0; i < 6; i++) {
     size_t s = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant