Skip to content

Commit 00d674c

Browse files
committed
kconfig: refactor conf_write_dep()
The if ... else inside the for-loop is unneeded because one empty line is placed after printing the last element of deps_config. Currently, all errors in conf_write_dep() are ignored. Add proper error checks. Rename it to conf_write_autoconf_cmd(), which is more intuitive. Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 57ddd07 commit 00d674c

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

scripts/kconfig/confdata.c

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -957,32 +957,50 @@ int conf_write(const char *name)
957957
}
958958

959959
/* write a dependency file as used by kbuild to track dependencies */
960-
static int conf_write_dep(const char *name)
960+
static int conf_write_autoconf_cmd(const char *autoconf_name)
961961
{
962+
char name[PATH_MAX], tmp[PATH_MAX];
962963
struct file *file;
963964
FILE *out;
965+
int ret;
964966

965-
out = fopen("..config.tmp", "w");
966-
if (!out)
967-
return 1;
968-
fprintf(out, "deps_config := \\\n");
969-
for (file = file_list; file; file = file->next) {
970-
if (file->next)
971-
fprintf(out, "\t%s \\\n", file->name);
972-
else
973-
fprintf(out, "\t%s\n", file->name);
967+
ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
968+
if (ret >= sizeof(name)) /* check truncation */
969+
return -1;
970+
971+
if (make_parent_dir(name))
972+
return -1;
973+
974+
ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
975+
if (ret >= sizeof(tmp)) /* check truncation */
976+
return -1;
977+
978+
out = fopen(tmp, "w");
979+
if (!out) {
980+
perror("fopen");
981+
return -1;
974982
}
975-
fprintf(out, "\n%s: \\\n"
976-
"\t$(deps_config)\n\n", conf_get_autoconfig_name());
977983

978-
env_write_dep(out, conf_get_autoconfig_name());
984+
fprintf(out, "deps_config := \\\n");
985+
for (file = file_list; file; file = file->next)
986+
fprintf(out, "\t%s \\\n", file->name);
987+
988+
fprintf(out, "\n%s: $(deps_config)\n\n", autoconf_name);
989+
990+
env_write_dep(out, autoconf_name);
979991

980992
fprintf(out, "\n$(deps_config): ;\n");
993+
994+
if (ferror(out)) /* error check for all fprintf() calls */
995+
return -1;
996+
981997
fclose(out);
982998

983-
if (make_parent_dir(name))
984-
return 1;
985-
rename("..config.tmp", name);
999+
if (rename(tmp, name)) {
1000+
perror("rename");
1001+
return -1;
1002+
}
1003+
9861004
return 0;
9871005
}
9881006

@@ -1109,7 +1127,9 @@ int conf_write_autoconf(int overwrite)
11091127
if (!overwrite && is_present(autoconf_name))
11101128
return 0;
11111129

1112-
conf_write_dep("include/config/auto.conf.cmd");
1130+
ret = conf_write_autoconf_cmd(autoconf_name);
1131+
if (ret)
1132+
return -1;
11131133

11141134
if (conf_touch_deps())
11151135
return 1;

0 commit comments

Comments
 (0)