diff mbox series

[dwarves,v3,3/5] btf_encoder: Traverse sections using a for-loop

Message ID 20210205134221.2953163-4-gprocida@google.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series ELF writing changes | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Giuliano Procida Feb. 5, 2021, 1:42 p.m. UTC
The pointer (iterator) scn can be made local to the loop and a more
general while-loop is not needed.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 libbtf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Andrii Nakryiko Feb. 8, 2021, 10:24 p.m. UTC | #1
On Fri, Feb 5, 2021 at 5:42 AM Giuliano Procida <gprocida@google.com> wrote:
>
> The pointer (iterator) scn can be made local to the loop and a more
> general while-loop is not needed.
>
> Signed-off-by: Giuliano Procida <gprocida@google.com>
> ---
>  libbtf.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/libbtf.c b/libbtf.c
> index ace8896..4ae7150 100644
> --- a/libbtf.c
> +++ b/libbtf.c
> @@ -700,7 +700,6 @@ static int btf_elf__write(const char *filename, struct btf *btf)
>  {
>         GElf_Ehdr ehdr;
>         Elf_Data *btf_data = NULL;
> -       Elf_Scn *scn = NULL;
>         Elf *elf = NULL;
>         const void *raw_btf_data;
>         uint32_t raw_btf_size;
> @@ -748,7 +747,7 @@ static int btf_elf__write(const char *filename, struct btf *btf)
>          */
>
>         elf_getshdrstrndx(elf, &strndx);
> -       while ((scn = elf_nextscn(elf, scn)) != NULL) {

this is pretty "canonical" as far as libelf usage goes, I wouldn't
touch this code, but it's up to Arnaldo


> +       for (Elf_Scn *scn = elf_nextscn(elf, NULL); scn; scn = elf_nextscn(elf, scn)) {
>                 GElf_Shdr shdr;
>                 if (!gelf_getshdr(scn, &shdr))
>                         continue;
> --
> 2.30.0.478.g8a0d178c01-goog
>
Giuliano Procida Feb. 9, 2021, 2:59 p.m. UTC | #2
Hi.

On Mon, 8 Feb 2021 at 22:24, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Feb 5, 2021 at 5:42 AM Giuliano Procida <gprocida@google.com> wrote:
> >
> > The pointer (iterator) scn can be made local to the loop and a more
> > general while-loop is not needed.
> >
> > Signed-off-by: Giuliano Procida <gprocida@google.com>
> > ---
> >  libbtf.c | 3 +--
> >  1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/libbtf.c b/libbtf.c
> > index ace8896..4ae7150 100644
> > --- a/libbtf.c
> > +++ b/libbtf.c
> > @@ -700,7 +700,6 @@ static int btf_elf__write(const char *filename, struct btf *btf)
> >  {
> >         GElf_Ehdr ehdr;
> >         Elf_Data *btf_data = NULL;
> > -       Elf_Scn *scn = NULL;
> >         Elf *elf = NULL;
> >         const void *raw_btf_data;
> >         uint32_t raw_btf_size;
> > @@ -748,7 +747,7 @@ static int btf_elf__write(const char *filename, struct btf *btf)
> >          */
> >
> >         elf_getshdrstrndx(elf, &strndx);
> > -       while ((scn = elf_nextscn(elf, scn)) != NULL) {
>
> this is pretty "canonical" as far as libelf usage goes, I wouldn't
> touch this code, but it's up to Arnaldo
>

Ack.
In an intermediate version of the code, I got bitten when I used scn
by mistake instead of another pointer.
This wouldn't have compiled if scn had been scoped to the loop.

Giuliano.

>
> > +       for (Elf_Scn *scn = elf_nextscn(elf, NULL); scn; scn = elf_nextscn(elf, scn)) {
> >                 GElf_Shdr shdr;
> >                 if (!gelf_getshdr(scn, &shdr))
> >                         continue;
> > --
> > 2.30.0.478.g8a0d178c01-goog
> >
diff mbox series

Patch

diff --git a/libbtf.c b/libbtf.c
index ace8896..4ae7150 100644
--- a/libbtf.c
+++ b/libbtf.c
@@ -700,7 +700,6 @@  static int btf_elf__write(const char *filename, struct btf *btf)
 {
 	GElf_Ehdr ehdr;
 	Elf_Data *btf_data = NULL;
-	Elf_Scn *scn = NULL;
 	Elf *elf = NULL;
 	const void *raw_btf_data;
 	uint32_t raw_btf_size;
@@ -748,7 +747,7 @@  static int btf_elf__write(const char *filename, struct btf *btf)
 	 */
 
 	elf_getshdrstrndx(elf, &strndx);
-	while ((scn = elf_nextscn(elf, scn)) != NULL) {
+	for (Elf_Scn *scn = elf_nextscn(elf, NULL); scn; scn = elf_nextscn(elf, scn)) {
 		GElf_Shdr shdr;
 		if (!gelf_getshdr(scn, &shdr))
 			continue;