Commits

jas43  committed 5d46cd3

Fix uninitialized access in vector routines.

Doing things like

ErrCode = ibset(ErrCode, aoterr_NonExistent)

is dangerous if ErrCode is not already set (and, for ErrCode to be meaningful
given the error code definitions, set to 0 initially). Change such lines to
(e.g.)

ErrCode = ibset(0, aoterr_NonExistent)

if that's the first time ErrCode can be set in the procedure or explicitly set
all ErrCode elements to 0 (depending on which is most convenient).

  • Participants
  • Parent commits 36970e8

Comments (0)

Files changed (3)

File source/aot_vector_module.f90

     integer :: vect_lb
     integer :: iComp
 
-    ! Try to interpret the top entry on the stack as a table
-    vect_handle = aot_table_top(L=L)
-    table_len = aot_table_length(L=L, thandle=vect_handle)
-
-    ! The size of the vector is limited by maxlength.
-    vect_len = min(maxlength, table_len)
-
-    ! Find the length of the default value, if it is not provided, its 0.
-    def_len = 0
-    if (present(default)) def_len = size(default)
-
-    ! Now parse the table with the vector entries.
-    if (aot_table_first(L, vect_handle)) then
-      allocate(val(vect_len))
-      allocate(errCode(vect_len))
-
-      ! Only if the vector table actually exists, and has at least one entry,
-      ! this parsing has to be done.
-      if (present(default).and.(def_len > 0)) then
-        call aot_top_get_val(val(1), ErrCode(1), L, default(1))
-      else
-        call aot_top_get_val(val(1), ErrCode(1), L)
-      end if
-
-      ! Up to the length of the default value, provide the default settings.
-      do iComp=2,def_len
-        if (.not. flu_next(L, vect_handle)) exit
-        call aot_top_get_val(val(iComp), ErrCode(iComp), L, &
-          &                  default(iComp))
-      end do
-
-      vect_lb = max(2, def_len+1)
-      ! After def_len entries no default values for the components are
-      ! available anymore, proceed without a default setting for the rest.
-      do iComp=vect_lb,vect_len
-        if (.not. flu_next(L, vect_handle)) exit
-        call aot_top_get_val(val(iComp), ErrCode(iComp), L)
-      end do
-    else
-      ! No vector definition found in the Lua script, use the default.
-      if (present(default)) then
-        allocate(val(def_len))
-        allocate(errCode(vect_len))
-        val = default
-        ErrCode = ibSet(ErrCode, aoterr_NonExistent)
-      else
-        ! No vector definition in the Lua script and no default provided,
-        ! return an empty array.
-        allocate(val(0))
-        allocate(errCode(0))
-      end if
-    end if
-    call aot_table_close(L, vect_handle)
-
-  end subroutine get_top_real_vvect
-
-
-
-  subroutine get_top_double_vvect(val, ErrCode, maxlength, L, default)
-    type(flu_State) :: L !< Handle to the lua script
-
-    !> Vector read from the Lua table, will have the same length as the table
-    !! but not exceed maxlength, if provided.
-    real(kind=double_k), intent(out), allocatable :: val(:)
-
-    !> Error code describing problems encountered in each of the components.
-    !! Will be allocated with the same length as the returned vector.
-    !! If the complete vector is not given in the Lua script, and no default
-    !! is provided, an zerosized array will be returned.
-    integer, intent(out), allocatable :: ErrCode(:)
-
-    !> Maximal length to allocate for the vector.
-    integer, intent(in) :: maxlength
-
-    !> A default vector to use, if no proper definition is found.
-    !! Components will be filled with the help of this default definition.
-    real(kind=double_k), intent(in), optional :: default(:)
-
-    integer :: vect_handle
-    integer :: table_len, vect_len, def_len
-    integer :: vect_lb
-    integer :: iComp
+    ErrCode = 0
 
     ! Try to interpret the top entry on the stack as a table
     vect_handle = aot_table_top(L=L)
         allocate(val(def_len))
         allocate(errCode(vect_len))
         val = default
-        ErrCode = ibSet(ErrCode, aoterr_NonExistent)
+        ErrCode = ibSet(0, aoterr_NonExistent)
       else
         ! No vector definition in the Lua script and no default provided,
         ! return an empty array.
     end if
     call aot_table_close(L, vect_handle)
 
-  end subroutine get_top_double_vvect
+  end subroutine get_top_real_vvect
 
 
-  subroutine get_top_integer_vvect(val, ErrCode, maxlength, L, default)
+
+  subroutine get_top_double_vvect(val, ErrCode, maxlength, L, default)
     type(flu_State) :: L !< Handle to the lua script
 
     !> Vector read from the Lua table, will have the same length as the table
     !! but not exceed maxlength, if provided.
-    integer, intent(out), allocatable :: val(:)
+    real(kind=double_k), intent(out), allocatable :: val(:)
 
     !> Error code describing problems encountered in each of the components.
     !! Will be allocated with the same length as the returned vector.
 
     !> A default vector to use, if no proper definition is found.
     !! Components will be filled with the help of this default definition.
-    integer, intent(in), optional :: default(:)
+    real(kind=double_k), intent(in), optional :: default(:)
 
     integer :: vect_handle
     integer :: table_len, vect_len, def_len
     integer :: vect_lb
     integer :: iComp
 
+    ErrCode = 0
+
     ! Try to interpret the top entry on the stack as a table
     vect_handle = aot_table_top(L=L)
     table_len = aot_table_length(L=L, thandle=vect_handle)
         allocate(val(def_len))
         allocate(errCode(vect_len))
         val = default
-        ErrCode = ibSet(ErrCode, aoterr_NonExistent)
+        ErrCode = ibSet(0, aoterr_NonExistent)
       else
         ! No vector definition in the Lua script and no default provided,
         ! return an empty array.
     end if
     call aot_table_close(L, vect_handle)
 
-  end subroutine get_top_integer_vvect
+  end subroutine get_top_double_vvect
 
 
-  subroutine get_top_long_vvect(val, ErrCode, maxlength, L, default)
+  subroutine get_top_integer_vvect(val, ErrCode, maxlength, L, default)
     type(flu_State) :: L !< Handle to the lua script
 
     !> Vector read from the Lua table, will have the same length as the table
     !! but not exceed maxlength, if provided.
-    integer(kind=long_k), intent(out), allocatable :: val(:)
+    integer, intent(out), allocatable :: val(:)
 
     !> Error code describing problems encountered in each of the components.
     !! Will be allocated with the same length as the returned vector.
 
     !> A default vector to use, if no proper definition is found.
     !! Components will be filled with the help of this default definition.
-    integer(kind=long_k), intent(in), optional :: default(:)
+    integer, intent(in), optional :: default(:)
 
     integer :: vect_handle
     integer :: table_len, vect_len, def_len
     integer :: vect_lb
     integer :: iComp
 
+    ErrCode = 0
+
     ! Try to interpret the top entry on the stack as a table
     vect_handle = aot_table_top(L=L)
     table_len = aot_table_length(L=L, thandle=vect_handle)
         allocate(val(def_len))
         allocate(errCode(vect_len))
         val = default
-        ErrCode = ibSet(ErrCode, aoterr_NonExistent)
+        ErrCode = ibSet(0, aoterr_NonExistent)
       else
         ! No vector definition in the Lua script and no default provided,
         ! return an empty array.
     end if
     call aot_table_close(L, vect_handle)
 
-  end subroutine get_top_long_vvect
+  end subroutine get_top_integer_vvect
 
 
-  subroutine get_top_logical_vvect(val, ErrCode, maxlength, L, default)
+  subroutine get_top_long_vvect(val, ErrCode, maxlength, L, default)
     type(flu_State) :: L !< Handle to the lua script
 
     !> Vector read from the Lua table, will have the same length as the table
     !! but not exceed maxlength, if provided.
-    logical, intent(out), allocatable :: val(:)
+    integer(kind=long_k), intent(out), allocatable :: val(:)
 
     !> Error code describing problems encountered in each of the components.
     !! Will be allocated with the same length as the returned vector.
 
     !> A default vector to use, if no proper definition is found.
     !! Components will be filled with the help of this default definition.
-    logical, intent(in), optional :: default(:)
+    integer(kind=long_k), intent(in), optional :: default(:)
 
     integer :: vect_handle
     integer :: table_len, vect_len, def_len
     integer :: vect_lb
     integer :: iComp
 
+    ErrCode = 0
+
     ! Try to interpret the top entry on the stack as a table
     vect_handle = aot_table_top(L=L)
     table_len = aot_table_length(L=L, thandle=vect_handle)
         allocate(val(def_len))
         allocate(errCode(vect_len))
         val = default
-        ErrCode = ibSet(ErrCode, aoterr_NonExistent)
+        ErrCode = ibSet(0, aoterr_NonExistent)
+      else
+        ! No vector definition in the Lua script and no default provided,
+        ! return an empty array.
+        allocate(val(0))
+        allocate(errCode(0))
+      end if
+    end if
+    call aot_table_close(L, vect_handle)
+
+  end subroutine get_top_long_vvect
+
+
+  subroutine get_top_logical_vvect(val, ErrCode, maxlength, L, default)
+    type(flu_State) :: L !< Handle to the lua script
+
+    !> Vector read from the Lua table, will have the same length as the table
+    !! but not exceed maxlength, if provided.
+    logical, intent(out), allocatable :: val(:)
+
+    !> Error code describing problems encountered in each of the components.
+    !! Will be allocated with the same length as the returned vector.
+    !! If the complete vector is not given in the Lua script, and no default
+    !! is provided, an zerosized array will be returned.
+    integer, intent(out), allocatable :: ErrCode(:)
+
+    !> Maximal length to allocate for the vector.
+    integer, intent(in) :: maxlength
+
+    !> A default vector to use, if no proper definition is found.
+    !! Components will be filled with the help of this default definition.
+    logical, intent(in), optional :: default(:)
+
+    integer :: vect_handle
+    integer :: table_len, vect_len, def_len
+    integer :: vect_lb
+    integer :: iComp
+
+    ErrCode = 0
+
+    ! Try to interpret the top entry on the stack as a table
+    vect_handle = aot_table_top(L=L)
+    table_len = aot_table_length(L=L, thandle=vect_handle)
+
+    ! The size of the vector is limited by maxlength.
+    vect_len = min(maxlength, table_len)
+
+    ! Find the length of the default value, if it is not provided, its 0.
+    def_len = 0
+    if (present(default)) def_len = size(default)
+
+    ! Now parse the table with the vector entries.
+    if (aot_table_first(L, vect_handle)) then
+      allocate(val(vect_len))
+      allocate(errCode(vect_len))
+
+      ! Only if the vector table actually exists, and has at least one entry,
+      ! this parsing has to be done.
+      if (present(default).and.(def_len > 0)) then
+        call aot_top_get_val(val(1), ErrCode(1), L, default(1))
+      else
+        call aot_top_get_val(val(1), ErrCode(1), L)
+      end if
+
+      ! Up to the length of the default value, provide the default settings.
+      do iComp=2,def_len
+        if (.not. flu_next(L, vect_handle)) exit
+        call aot_top_get_val(val(iComp), ErrCode(iComp), L, &
+          &                  default(iComp))
+      end do
+
+      vect_lb = max(2, def_len+1)
+      ! After def_len entries no default values for the components are
+      ! available anymore, proceed without a default setting for the rest.
+      do iComp=vect_lb,vect_len
+        if (.not. flu_next(L, vect_handle)) exit
+        call aot_top_get_val(val(iComp), ErrCode(iComp), L)
+      end do
+    else
+      ! No vector definition found in the Lua script, use the default.
+      if (present(default)) then
+        allocate(val(def_len))
+        allocate(errCode(vect_len))
+        val = default
+        ErrCode = ibSet(0, aoterr_NonExistent)
       else
         ! No vector definition in the Lua script and no default provided,
         ! return an empty array.
     integer :: vect_lb
     integer :: iComp
 
+    ErrCode = 0
+
     ! Try to interpret it as table.
     vect_handle = aot_table_top(L=L)
     table_len = aot_table_length(L=L, thandle=vect_handle)
     vect_handle = aot_table_top(L=L)
     table_len = aot_table_length(L=L, thandle=vect_handle)
 
+    ErrCode = 0
+
     vect_len = min(table_len, size(val))
 
     ! Find the length of the default value, if it is not provided, its 0.
     integer :: vect_lb
     integer :: iComp
 
+    ErrCode = 0
+
     ! Try to interpret it as table.
     vect_handle = aot_table_top(L=L)
     table_len = aot_table_length(L=L, thandle=vect_handle)
     integer :: vect_lb
     integer :: iComp
 
+    ErrCode = 0
+
     ! Try to interpret it as table.
     vect_handle = aot_table_top(L=L)
     table_len = aot_table_length(L=L, thandle=vect_handle)
     integer :: vect_lb
     integer :: iComp
 
+    ErrCode = 0
+
     ! Try to interpret it as table.
     vect_handle = aot_table_top(L=L)
     table_len = aot_table_length(L=L, thandle=vect_handle)

File source/extdouble/aot_extdouble_vector_module.f90

         allocate(val(def_len))
         allocate(errCode(vect_len))
         val = default
-        ErrCode = ibSet(ErrCode, aoterr_NonExistent)
+        ErrCode = ibSet(0, aoterr_NonExistent)
       else
         ! No vector definition in the Lua script and no default provided,
         ! return an empty array.
     integer :: vect_lb
     integer :: iComp
 
+    ErrCode = 0
+
     ! Try to interpret it as table.
     vect_handle = aot_table_top(L=L)
     table_len = aot_table_length(L=L, thandle=vect_handle)

File source/quadruple/aot_quadruple_vector_module.f90

         allocate(val(def_len))
         allocate(errCode(vect_len))
         val = default
-        ErrCode = ibSet(ErrCode, aoterr_NonExistent)
+        ErrCode = ibSet(0, aoterr_NonExistent)
       else
         ! No vector definition in the Lua script and no default provided,
         ! return an empty array.
     integer :: vect_lb
     integer :: iComp
 
+    ErrCode = 0
+
     ! Try to interpret it as table.
     vect_handle = aot_table_top(L=L)
     table_len = aot_table_length(L=L, thandle=vect_handle)