gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnunet-scheme] 118/324: netstruct: Fix field lookup and offset calculat


From: gnunet
Subject: [gnunet-scheme] 118/324: netstruct: Fix field lookup and offset calculation.
Date: Tue, 21 Sep 2021 13:22:38 +0200

This is an automated email from the git hooks/post-receive script.

maxime-devos pushed a commit to branch master
in repository gnunet-scheme.

commit 22153b9827f87660fd0417004099737a930b623a
Author: Maxime Devos <maximedevos@telenet.be>
AuthorDate: Sat May 29 19:09:43 2021 +0200

    netstruct: Fix field lookup and offset calculation.
    
    The first issue is a wrong comparison: (> i vlen)
    instead of (< i vlen). A second issue is an ambigious
    variable name, where the wrong variable is used.
    A third issue is forgetting to actually return the
    field during field lookup.
    
    * tests/netstruct.scm: Test it.
    * gnu/gnunet/netstruct/procedural.scm (vtable/struct)
      [offsetof]: Correct bounds check and disambiguate variable names.
      [part]: Actually return the field.
---
 Makefile.am                         |  3 ++-
 gnu/gnunet/netstruct/procedural.scm | 13 +++++++------
 tests/netstruct.scm                 | 31 +++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 51273bf..8b15711 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -105,7 +105,8 @@ SCM_TESTS = \
   tests/config-parser.scm \
   tests/config-value-parser.scm \
   tests/config-expander.scm \
-  tests/config-db.scm
+  tests/config-db.scm \
+  tests/netstruct.scm
 
 SCM_TESTS_ENVIRONMENT = \
   GUILE_AUTO_COMPILE=0 \
diff --git a/gnu/gnunet/netstruct/procedural.scm 
b/gnu/gnunet/netstruct/procedural.scm
index 2a38db7..0db2d6d 100644
--- a/gnu/gnunet/netstruct/procedural.scm
+++ b/gnu/gnunet/netstruct/procedural.scm
@@ -284,19 +284,20 @@ structure @var{ns} in the bytevector slice @var{ns}."
          (let* ((vec (%netstruct-fields ns))
                 (vlen (vector-length vec)))
            (let loop ((i 0) (off 0))
-             (unless (> i vlen)
+             (unless (< i vlen)
                (no-such-field 'offsetof))
-             (let* ((field (vector-ref vec i))
-                    (fsize (%size (field-type field))))
-               (if (eq? (field-name field) field)
+             (let* ((field-found (vector-ref vec i))
+                    (field-size (%size (field-type field-found))))
+               (if (eq? (field-name field-found) field)
                    off
                    (loop (+ i 1)
-                         (+ off fsize)))))))
+                         (+ off field-size)))))))
 
        (define (part ns field)
          (let* ((vec (%netstruct-fields ns)))
            (or (vector-any (lambda (f)
-                             (eq? (field-name f) field))
+                             (and (eq? (field-name f) field)
+                                  (field-type f)))
                            vec)
                (no-such-field 'part))))
        (make-netstruct-vtable
diff --git a/tests/netstruct.scm b/tests/netstruct.scm
new file mode 100644
index 0000000..5a2bca8
--- /dev/null
+++ b/tests/netstruct.scm
@@ -0,0 +1,31 @@
+;; This file is part of scheme-GNUnet.
+;; Copyright (C) 2021 Maxime Devos
+;;
+;; scheme-GNUnet is free software: you can redistribute it and/or modify it
+;; under the terms of the GNU Affero General Public License as published
+;; by the Free Software Foundation, either version 3 of the License,
+;; or (at your option) any later version.
+;;
+;; scheme-GNUnet is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; Affero General Public License for more details.
+;;
+;; You should have received a copy of the GNU Affero General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+;;
+;; SPDX-License-Identifier: AGPL3.0-or-later
+(import (srfi srfi-26)
+       (gnu gnunet netstruct procedural))
+
+(test-begin "netstruct procedural")
+
+(define struct/a
+  (make-netstructure
+   (vector (make-field 'field u8))))
+
+(test-eqv "first field, offset (struct)"
+  0
+  (offsetof struct/a '(field)))
+
+(test-end "netstruct procedural")

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]