Skip to contentSkip to conversation

modpost: use strstarts() to clean up parse_source_files()

PATCH In review Masahiro Yamada 2 participants 4 messages Feb 7, 2025 – Feb 11, 2025 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 modpost: use strstarts() to clean up parse_source_files()

No functional changes are intended.

---

 scripts/mod/sumversion.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/mod/sumversion.c +2 -2
index e79fc40d852f..3dd28b4d0099 100644
@@ -330,7 +330,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
 			line++;
 		p = line;
 
-		if (strncmp(line, "source_", sizeof("source_")-1) == 0) {
+		if (strstarts(line, "source_")) {
 			p = strrchr(line, ' ');
 			if (!p) {
 				warn("malformed line: %s\n", line);
@@ -344,7 +344,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
 			}
 			continue;
 		}
-		if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
+		if (strstarts(line, "deps_")) {
 			check_files = 1;
 			continue;
 		}
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 33 lines · Masahiro Yamada

No functional changes are intended.

---

 scripts/mod/sumversion.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/mod/sumversion.c +2 -2
index e79fc40d852f..3dd28b4d0099 100644
@@ -330,7 +330,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
 			line++;
 		p = line;
 
-		if (strncmp(line, "source_", sizeof("source_")-1) == 0) {
+		if (strstarts(line, "source_")) {
 			p = strrchr(line, ' ');
 			if (!p) {
 				warn("malformed line: %s\n", line);
@@ -344,7 +344,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
 			}
 			continue;
 		}
-		if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
+		if (strstarts(line, "deps_")) {
 			check_files = 1;
 			continue;
 		}
Signature · 1 line
2.43.0

Thanks, looks good to me.

Do you also want to replace the last strncmp() in
scripts/mod/modpost.c?

scripts/mod/modpost.c +1 -1
index e18ae7dc8140..31468923cdf6 100644
@@ -670,7 +670,7 @@ static char *get_next_modinfo(struct elf_info *info, const char *tag,
        }
 
        for (p = modinfo; p; p = next_string(p, &size)) {
-               if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
+               if (strstarts(p, tag) == 0 && p[taglen] == '=')
                        return p + taglen + 1;
        }
        return NULL;

Kind regards,
Nicolas

#2 lore raw
Quoted 48 lines · Nicolas Schier
Quoted 31 lines · Masahiro Yamada

No functional changes are intended.

---

 scripts/mod/sumversion.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/mod/sumversion.c +2 -3
index e79fc40d852f..3dd28b4d0099 100644
@@ -330,7 +330,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
                      line++;
              p = line;
 
-             if (strncmp(line, "source_", sizeof("source_")-1) == 0) {
+             if (strstarts(line, "source_")) {
                      p = strrchr(line, ' ');
                      if (!p) {
                              warn("malformed line: %s\n", line);
@@ -344,7 +344,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
                      }
                      continue;
              }
-             if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
+             if (strstarts(line, "deps_")) {
                      check_files = 1;
                      continue;
              }
--
Quoted 2 lines

2.43.0

Thanks, looks good to me.

Do you also want to replace the last strncmp() in
scripts/mod/modpost.c?

scripts/mod/modpost.c +0 -0
index e18ae7dc8140..31468923cdf6 100644

scripts/mod/modpost.c static char *get_next_modinfo(struct elf_info *info, const char *tag, · line 676

Quoted 8 lines
scripts/mod/modpost.c +1 -1
@@ -670,7 +670,7 @@ static char *get_next_modinfo(struct elf_info *info, const char *tag,
        }
 
        for (p = modinfo; p; p = next_string(p, &size)) {
-               if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
+               if (strstarts(p, tag) == 0 && p[taglen] == '=')
                        return p + taglen + 1;
        }
        return NULL;

I believe you meant:

  if (strstarts(p, tag) && p[taglen] == '=')

I do not think there is a strong reason to do so because taglen is already calculated a few lines above, but the compiler may be clever enough to avoid calling strlen() twice. I did not check the compiler output.

Signature · 2 lines
Best Regards
Masahiro Yamada
#3 lore raw
Quoted 61 lines · On Tue, Feb 11, 2025 at 09:43:05AM +0900 Masahiro Yamada wrote:
Quoted 48 lines · Nicolas Schier
Quoted 31 lines · Masahiro Yamada

No functional changes are intended.

---

 scripts/mod/sumversion.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/mod/sumversion.c +2 -3
index e79fc40d852f..3dd28b4d0099 100644
@@ -330,7 +330,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
                      line++;
              p = line;
 
-             if (strncmp(line, "source_", sizeof("source_")-1) == 0) {
+             if (strstarts(line, "source_")) {
                      p = strrchr(line, ' ');
                      if (!p) {
                              warn("malformed line: %s\n", line);
@@ -344,7 +344,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
                      }
                      continue;
              }
-             if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
+             if (strstarts(line, "deps_")) {
                      check_files = 1;
                      continue;
              }
--
Quoted 2 lines

2.43.0

Thanks, looks good to me.

Do you also want to replace the last strncmp() in
scripts/mod/modpost.c?

scripts/mod/modpost.c +0 -0
index e18ae7dc8140..31468923cdf6 100644

scripts/mod/modpost.c static char *get_next_modinfo(struct elf_info *info, const char *tag, · line 676

Quoted 8 lines
scripts/mod/modpost.c +1 -1
@@ -670,7 +670,7 @@ static char *get_next_modinfo(struct elf_info *info, const char *tag,
        }
 
        for (p = modinfo; p; p = next_string(p, &size)) {
-               if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
+               if (strstarts(p, tag) == 0 && p[taglen] == '=')
                        return p + taglen + 1;
        }
        return NULL;

I believe you meant:

Quoted 1 line
  if (strstarts(p, tag) && p[taglen] == '=')

ups, yes.

Quoted 4 lines

I do not think there is a strong reason to do so because taglen is already calculated a few lines above, but the compiler may be clever enough to avoid calling strlen() twice. I did not check the compiler output.

yeah, I was thinking about code (style) consistency, but you're right, it would not be beneficial.

Kind regards,
Nicolas