]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
robustify: libelf gelf define INVALID_NDX macro.
authorJakub Jelinek <jakub@redhat.com>
Fri, 17 Jan 2014 19:38:01 +0000 (20:38 +0100)
committerMark Wielaard <mjw@redhat.com>
Thu, 23 Jan 2014 10:31:53 +0000 (11:31 +0100)
Signed-off-by: Mark Wielaard <mjw@redhat.com>
20 files changed:
libelf/ChangeLog
libelf/gelf_getdyn.c
libelf/gelf_getlib.c
libelf/gelf_getmove.c
libelf/gelf_getrel.c
libelf/gelf_getrela.c
libelf/gelf_getsym.c
libelf/gelf_getsyminfo.c
libelf/gelf_getsymshndx.c
libelf/gelf_getversym.c
libelf/gelf_update_dyn.c
libelf/gelf_update_lib.c
libelf/gelf_update_move.c
libelf/gelf_update_rel.c
libelf/gelf_update_rela.c
libelf/gelf_update_sym.c
libelf/gelf_update_syminfo.c
libelf/gelf_update_symshndx.c
libelf/gelf_update_versym.c
libelf/libelfP.h

index ee070e050042d4ce7a38442028c85f672b280df9..c67443b16a6adecb250b39308072cbe97efb6f9e 100644 (file)
@@ -1,3 +1,26 @@
+2014-01-17  Jakub Jelinek  <jakub@redhat.com>
+           Roland McGrath  <roland@redhat.com>
+
+       * libelfP.h (INVALID_NDX): Define.
+       * gelf_getdyn.c (gelf_getdyn): Use it.  Remove ndx < 0 test if any.
+       * gelf_getlib.c (gelf_getlib): Likewise.
+       * gelf_getmove.c (gelf_getmove): Likewise.
+       * gelf_getrel.c (gelf_getrel): Likewise.
+       * gelf_getrela.c (gelf_getrela): Likewise.
+       * gelf_getsym.c (gelf_getsym): Likewise.
+       * gelf_getsyminfo.c (gelf_getsyminfo): Likewise.
+       * gelf_getsymshndx.c (gelf_getsymshndx): Likewise.
+       * gelf_getversym.c (gelf_getversym): Likewise.
+       * gelf_update_dyn.c (gelf_update_dyn): Likewise.
+       * gelf_update_lib.c (gelf_update_lib): Likewise.
+       * gelf_update_move.c (gelf_update_move): Likewise.
+       * gelf_update_rel.c (gelf_update_rel): Likewise.
+       * gelf_update_rela.c (gelf_update_rela): Likewise.
+       * gelf_update_sym.c (gelf_update_sym): Likewise.
+       * gelf_update_syminfo.c (gelf_update_syminfo): Likewise.
+       * gelf_update_symshndx.c (gelf_update_symshndx): Likewise.
+       * gelf_update_versym.c (gelf_update_versym): Likewise.
+
 2014-01-17  Jakub Jelinek  <jakub@redhat.com>
 
        * elf32_getphdr.c (elfw2(LIBELFBITS,getphdr)): Check if program header
index 721af62284dc69bb41ac440783ea5844f1e7e0cb..c366fd5df72d7e3b033e51dc0c76903cbbc09a0b 100644 (file)
@@ -1,5 +1,5 @@
 /* Get information from dynamic table at the given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -72,7 +72,7 @@ gelf_getdyn (data, ndx, dst)
         table entries has to be adopted.  The user better has provided
         a buffer where we can store the information.  While copying the
         data we are converting the format.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Dyn, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -93,7 +93,7 @@ gelf_getdyn (data, ndx, dst)
 
       /* The data is already in the correct form.  Just make sure the
         index is OK.  */
-      if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, GElf_Dyn, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index 9a8a17491812b9bbdb93b3f2313931739507d2f3..880817e65556849ade7523de1789c1010785c37e 100644 (file)
@@ -1,5 +1,5 @@
 /* Get library from table at the given index.
-   Copyright (C) 2004 Red Hat, Inc.
+   Copyright (C) 2004, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2004.
 
@@ -65,7 +65,7 @@ gelf_getlib (data, ndx, dst)
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
   GElf_Lib *result = NULL;
-  if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Lib, data))
     __libelf_seterrno (ELF_E_INVALID_INDEX);
   else
     {
index 95669303c3dfb80d842ae86e2b834c06bb840130..b81d61f4c7416c848c93fe96b92614dccc4087e2 100644 (file)
@@ -1,5 +1,5 @@
 /* Get move structure at the given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -62,7 +62,7 @@ gelf_getmove (data, ndx, dst)
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Move, data))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       goto out;
index 78394b425533519942de4490aa5b808ce2e4f68f..1f786ff5b38c31f5f1e4b3552c73c142ee6e8dfc 100644 (file)
@@ -1,5 +1,5 @@
 /* Get REL relocation information at given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -50,12 +50,6 @@ gelf_getrel (data, ndx, dst)
   if (data_scn == NULL)
     return NULL;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return NULL;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
     {
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
@@ -72,7 +66,7 @@ gelf_getrel (data, ndx, dst)
   if (scn->elf->class == ELFCLASS32)
     {
       /* We have to convert the data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rel, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          result = NULL;
@@ -92,7 +86,7 @@ gelf_getrel (data, ndx, dst)
     {
       /* Simply copy the data after we made sure we are actually getting
         correct data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rel, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          result = NULL;
index 1d0e7eee2da669492d53f41bddb1936fe55f2338..cead7eeecd13e3a739f43091f73debaff3d6d90a 100644 (file)
@@ -1,5 +1,5 @@
 /* Get RELA relocation information at given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -50,12 +50,6 @@ gelf_getrela (data, ndx, dst)
   if (data_scn == NULL)
     return NULL;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return NULL;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
     {
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
@@ -72,7 +66,7 @@ gelf_getrela (data, ndx, dst)
   if (scn->elf->class == ELFCLASS32)
     {
       /* We have to convert the data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rela, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          result = NULL;
@@ -93,7 +87,7 @@ gelf_getrela (data, ndx, dst)
     {
       /* Simply copy the data after we made sure we are actually getting
         correct data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rela, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          result = NULL;
index 183872e2fb2447ffb5ff410af709204908c30baf..a141c2d58a37e838f1c03416f62706c5392b1bab 100644 (file)
@@ -1,5 +1,5 @@
 /* Get symbol information from symbol table at the given index.
-   Copyright (C) 1999, 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 1999.
 
@@ -69,7 +69,7 @@ gelf_getsym (data, ndx, dst)
         table entries has to be adopted.  The user better has provided
         a buffer where we can store the information.  While copying the
         data we are converting the format.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym, data))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -98,7 +98,7 @@ gelf_getsym (data, ndx, dst)
 
       /* The data is already in the correct form.  Just make sure the
         index is OK.  */
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
+      if (INVALID_NDX (ndx, GElf_Sym, data))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index 09c9cee0b702e6783542aa5ebd932352336094c1..8d7da7f23b73db1ebaace9e3f9d0d87b175a510f 100644 (file)
@@ -1,5 +1,5 @@
 /* Get additional symbol information from symbol table at the given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -63,7 +63,7 @@ gelf_getsyminfo (data, ndx, dst)
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Syminfo, data))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       goto out;
index 91e1bf604f19c437e2a4a9ed48ba907d044773d5..c19e87616f5953ae3b76c5be1af825c9eab29262 100644 (file)
@@ -1,6 +1,6 @@
 /* Get symbol information and separate section index from symbol table
    at the given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -69,7 +69,7 @@ gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx)
      section index table.  */
   if (likely (shndxdata_scn != NULL))
     {
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Word, &shndxdata_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -89,7 +89,7 @@ gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx)
         table entries has to be adopted.  The user better has provided
         a buffer where we can store the information.  While copying the
         data we are converting the format.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym, symdata))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -118,7 +118,7 @@ gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx)
 
       /* The data is already in the correct form.  Just make sure the
         index is OK.  */
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
+      if (INVALID_NDX (ndx, GElf_Sym, symdata))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index 9e80efc02467ef0336a934c7450178e43369e476..fe8dc62bc5bc169b91c909330e42b7141b7b5715 100644 (file)
@@ -1,5 +1,5 @@
 /* Get symbol version information at the given index.
-   Copyright (C) 1999, 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 1999.
 
@@ -71,7 +71,7 @@ gelf_getversym (data, ndx, dst)
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Versym, data))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       result = NULL;
index 94408c8400e261cfe90bbf9da5f6ef7617634856..2eb526ec0fd4b686c1ff046733d520ef43d23f16 100644 (file)
@@ -1,5 +1,5 @@
 /* Update information in dynamic table at the given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -50,12 +50,6 @@ gelf_update_dyn (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_DYN))
     {
       /* The type of the data better should match.  */
@@ -81,7 +75,7 @@ gelf_update_dyn (data, ndx, src)
        }
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Dyn, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -95,7 +89,7 @@ gelf_update_dyn (data, ndx, src)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Dyn, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index 4700bef830238a229da60564cffbc2e4598a9375..1c8c23da91309787eafa3d91ee34d03e9db10fd8 100644 (file)
@@ -1,5 +1,5 @@
 /* Update library in table at the given index.
-   Copyright (C) 2004 Red Hat, Inc.
+   Copyright (C) 2004, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2004.
 
@@ -47,12 +47,6 @@ gelf_update_lib (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
   if (unlikely (data_scn->d.d_type != ELF_T_LIB))
     {
@@ -66,7 +60,7 @@ gelf_update_lib (data, ndx, src)
 
   /* Check whether we have to resize the data buffer.  */
   int result = 0;
-  if (unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
+  if (INVALID_NDX (ndx, Elf64_Lib, &data_scn->d))
     __libelf_seterrno (ELF_E_INVALID_INDEX);
   else
     {
index 102a45b85ca13fbf837f677bc3d636e5bf2c2850..ad2ca6abcb4f51dd71eb0294f769b695c71f8c85 100644 (file)
@@ -1,5 +1,5 @@
 /* Update move structure at the given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -54,8 +54,7 @@ gelf_update_move (data, ndx, src)
   assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
 
   /* Check whether we have to resize the data buffer.  */
-  if (unlikely (ndx < 0)
-      || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size))
+  if (INVALID_NDX (ndx, GElf_Move, &data_scn->d))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       return 0;
index d13e4028019e93228ba5dbee8b5e764a8f464887..14f62e973a5e816b4ea64faa992d78493d87e96e 100644 (file)
@@ -1,5 +1,5 @@
 /* Update REL relocation information at given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -47,12 +47,6 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
   if (dst == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
     {
       /* The type of the data better should match.  */
@@ -78,7 +72,7 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
        }
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rel, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -93,7 +87,7 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rel, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index cc70297e14d5b75de88b626517e712db41ffa014..88252703976f9cc84f167229d76cb4958180224a 100644 (file)
@@ -1,5 +1,5 @@
 /* Update RELA relocation information at given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -47,12 +47,6 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
   if (dst == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
     {
       /* The type of the data better should match.  */
@@ -80,7 +74,7 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
        }
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rela, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -96,7 +90,7 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rela, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index ab37641c05c28a95a431feaa59d40115b51a04d5..278129cfc70d358242069a172b50916c34ce0e9c 100644 (file)
@@ -1,5 +1,5 @@
 /* Update symbol information in symbol table at the given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -51,12 +51,6 @@ gelf_update_sym (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_SYM))
     {
       /* The type of the data better should match.  */
@@ -81,7 +75,7 @@ gelf_update_sym (data, ndx, src)
        }
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -104,7 +98,7 @@ gelf_update_sym (data, ndx, src)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Sym, &data_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index 5654a0d94de311f26a4243b7b70b3dd5c1eb5f52..640a1ed26ba36e3977d00d2a4bafbc3196908714 100644 (file)
@@ -1,5 +1,5 @@
 /* Update additional symbol information in symbol table at the given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -51,12 +51,6 @@ gelf_update_syminfo (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
     {
       /* The type of the data better should match.  */
@@ -72,7 +66,7 @@ gelf_update_syminfo (data, ndx, src)
   rwlock_wrlock (scn->elf->lock);
 
   /* Check whether we have to resize the data buffer.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
+  if (INVALID_NDX (ndx, GElf_Syminfo, &data_scn->d))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       goto out;
index d2bb5bbed8c99afa99893c0e64141fe0f76d8bd3..5e2c7f7035e647db32310cfad2030b8c640003c2 100644 (file)
@@ -1,6 +1,6 @@
 /* Update symbol information and section index in symbol table at the
    given index.
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -56,12 +56,6 @@ gelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx)
   if (symdata == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
     {
       /* The type of the data better should match.  */
@@ -107,7 +101,7 @@ gelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx)
        }
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym, &symdata_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -130,7 +124,7 @@ gelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Sym, &symdata_scn->d))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index a4cec1f3ec9a691eb743f960fce3a257776d80df..03a3c5a146e09510cdb6ecafd80a77da44b92f0d 100644 (file)
@@ -1,5 +1,5 @@
 /* Update symbol version information.
-   Copyright (C) 2001, 2002 Red Hat, Inc.
+   Copyright (C) 2001, 2002, 2005, 2009, 2014 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2001.
 
@@ -54,8 +54,7 @@ gelf_update_versym (data, ndx, src)
   assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
 
   /* Check whether we have to resize the data buffer.  */
-  if (unlikely (ndx < 0)
-      || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size))
+  if (INVALID_NDX (ndx, GElf_Versym, &data_scn->d))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       return 0;
index 7da237092383517233106ee6213f81363c40c1d0..52cf7457df25baa2cae1b626954599c18bc83712 100644 (file)
@@ -587,4 +587,8 @@ extern uint32_t __libelf_crc32 (uint32_t crc, unsigned char *buf, size_t len)
 /* Align offset to 4 bytes as needed for note name and descriptor data.  */
 #define NOTE_ALIGN(n)  (((n) + 3) & -4U)
 
+/* Convenience macro.  */
+#define INVALID_NDX(ndx, type, data) \
+  unlikely ((data)->d_size / sizeof (type) <= (unsigned int) (ndx))
+
 #endif  /* libelfP.h */