From: Jakub Jelinek Date: Fri, 17 Jan 2014 19:38:01 +0000 (+0100) Subject: robustify: libelf gelf define INVALID_NDX macro. X-Git-Tag: elfutils-0.159~48 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a9039623c272f458a53f09009c2a0e1d6e60491d;p=thirdparty%2Felfutils.git robustify: libelf gelf define INVALID_NDX macro. Signed-off-by: Mark Wielaard --- diff --git a/libelf/ChangeLog b/libelf/ChangeLog index ee070e050..c67443b16 100644 --- a/libelf/ChangeLog +++ b/libelf/ChangeLog @@ -1,3 +1,26 @@ +2014-01-17 Jakub Jelinek + Roland McGrath + + * 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 * elf32_getphdr.c (elfw2(LIBELFBITS,getphdr)): Check if program header diff --git a/libelf/gelf_getdyn.c b/libelf/gelf_getdyn.c index 721af6228..c366fd5df 100644 --- a/libelf/gelf_getdyn.c +++ b/libelf/gelf_getdyn.c @@ -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 , 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; diff --git a/libelf/gelf_getlib.c b/libelf/gelf_getlib.c index 9a8a17491..880817e65 100644 --- a/libelf/gelf_getlib.c +++ b/libelf/gelf_getlib.c @@ -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 , 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 { diff --git a/libelf/gelf_getmove.c b/libelf/gelf_getmove.c index 95669303c..b81d61f4c 100644 --- a/libelf/gelf_getmove.c +++ b/libelf/gelf_getmove.c @@ -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 , 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; diff --git a/libelf/gelf_getrel.c b/libelf/gelf_getrel.c index 78394b425..1f786ff5b 100644 --- a/libelf/gelf_getrel.c +++ b/libelf/gelf_getrel.c @@ -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 , 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; diff --git a/libelf/gelf_getrela.c b/libelf/gelf_getrela.c index 1d0e7eee2..cead7eeec 100644 --- a/libelf/gelf_getrela.c +++ b/libelf/gelf_getrela.c @@ -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 , 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; diff --git a/libelf/gelf_getsym.c b/libelf/gelf_getsym.c index 183872e2f..a141c2d58 100644 --- a/libelf/gelf_getsym.c +++ b/libelf/gelf_getsym.c @@ -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 , 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; diff --git a/libelf/gelf_getsyminfo.c b/libelf/gelf_getsyminfo.c index 09c9cee0b..8d7da7f23 100644 --- a/libelf/gelf_getsyminfo.c +++ b/libelf/gelf_getsyminfo.c @@ -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 , 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; diff --git a/libelf/gelf_getsymshndx.c b/libelf/gelf_getsymshndx.c index 91e1bf604..c19e87616 100644 --- a/libelf/gelf_getsymshndx.c +++ b/libelf/gelf_getsymshndx.c @@ -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 , 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; diff --git a/libelf/gelf_getversym.c b/libelf/gelf_getversym.c index 9e80efc02..fe8dc62bc 100644 --- a/libelf/gelf_getversym.c +++ b/libelf/gelf_getversym.c @@ -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 , 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; diff --git a/libelf/gelf_update_dyn.c b/libelf/gelf_update_dyn.c index 94408c840..2eb526ec0 100644 --- a/libelf/gelf_update_dyn.c +++ b/libelf/gelf_update_dyn.c @@ -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 , 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; diff --git a/libelf/gelf_update_lib.c b/libelf/gelf_update_lib.c index 4700bef83..1c8c23da9 100644 --- a/libelf/gelf_update_lib.c +++ b/libelf/gelf_update_lib.c @@ -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 , 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 { diff --git a/libelf/gelf_update_move.c b/libelf/gelf_update_move.c index 102a45b85..ad2ca6abc 100644 --- a/libelf/gelf_update_move.c +++ b/libelf/gelf_update_move.c @@ -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 , 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; diff --git a/libelf/gelf_update_rel.c b/libelf/gelf_update_rel.c index d13e40280..14f62e973 100644 --- a/libelf/gelf_update_rel.c +++ b/libelf/gelf_update_rel.c @@ -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 , 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; diff --git a/libelf/gelf_update_rela.c b/libelf/gelf_update_rela.c index cc70297e1..882527039 100644 --- a/libelf/gelf_update_rela.c +++ b/libelf/gelf_update_rela.c @@ -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 , 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; diff --git a/libelf/gelf_update_sym.c b/libelf/gelf_update_sym.c index ab37641c0..278129cfc 100644 --- a/libelf/gelf_update_sym.c +++ b/libelf/gelf_update_sym.c @@ -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 , 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; diff --git a/libelf/gelf_update_syminfo.c b/libelf/gelf_update_syminfo.c index 5654a0d94..640a1ed26 100644 --- a/libelf/gelf_update_syminfo.c +++ b/libelf/gelf_update_syminfo.c @@ -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 , 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; diff --git a/libelf/gelf_update_symshndx.c b/libelf/gelf_update_symshndx.c index d2bb5bbed..5e2c7f703 100644 --- a/libelf/gelf_update_symshndx.c +++ b/libelf/gelf_update_symshndx.c @@ -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 , 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; diff --git a/libelf/gelf_update_versym.c b/libelf/gelf_update_versym.c index a4cec1f3e..03a3c5a14 100644 --- a/libelf/gelf_update_versym.c +++ b/libelf/gelf_update_versym.c @@ -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 , 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; diff --git a/libelf/libelfP.h b/libelf/libelfP.h index 7da237092..52cf7457d 100644 --- a/libelf/libelfP.h +++ b/libelf/libelfP.h @@ -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 */