Skip to content

Commit

Permalink
Account for pointers being NULL when printf-ing
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknyquist committed Oct 27, 2023
1 parent e8a3b66 commit 78285e3
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion duckargs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def _generate_c_print_code(processed_args):
var_name = f"{arg.var_name}"
elif arg.type in [ArgType.FILE, ArgType.STRING]:
format_arg = "%s"
var_name = f"{arg.var_name}"
var_name = f"{arg.var_name} ? {arg.var_name} : \"null\""

ret += f" printf(\"{arg.var_name}: {format_arg}\\n\", {var_name});\n"

Expand Down
6 changes: 3 additions & 3 deletions tests/test_data/choices/expected_c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ int main(int argc, char *argv[])
return ret;
}

printf("pos1: %s\n", pos1);
printf("pos2: %s\n", pos2);
printf("pos1: %s\n", pos1 ? pos1 : "null");
printf("pos2: %s\n", pos2 ? pos2 : "null");
printf("f: %s\n", f ? "true" : "false");
printf("g: %s\n", g ? "true" : "false");
printf("qefqaf: %s\n", qefqaf);
printf("qefqaf: %s\n", qefqaf ? qefqaf : "null");

return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/test_data/env_comment/expected_c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ int main(int argc, char *argv[])
return ret;
}

printf("positional_arg1: %s\n", positional_arg1);
printf("positional_arg2: %s\n", positional_arg2);
printf("positional_arg1: %s\n", positional_arg1 ? positional_arg1 : "null");
printf("positional_arg2: %s\n", positional_arg2 ? positional_arg2 : "null");
printf("int_val: %ld\n", int_val);
printf("e: %.4f\n", e);
printf("file: %s\n", file);
printf("otherfile: %s\n", otherfile);
printf("file: %s\n", file ? file : "null");
printf("otherfile: %s\n", otherfile ? otherfile : "null");
printf("a: %s\n", a ? "true" : "false");
printf("b: %s\n", b ? "true" : "false");
printf("c: %s\n", c ? "true" : "false");
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data/hex/expected_c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ int main(int argc, char *argv[])
}

printf("fff: %ld\n", fff);
printf("q: %s\n", q);
printf("test: %s\n", test);
printf("q: %s\n", q ? q : "null");
printf("test: %s\n", test ? test : "null");

return 0;
}
Expand Down
28 changes: 14 additions & 14 deletions tests/test_data/many_opts/expected_c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,28 +227,28 @@ int main(int argc, char *argv[])
return ret;
}

printf("pos1: %s\n", pos1);
printf("pos2: %s\n", pos2);
printf("pos3: %s\n", pos3);
printf("pos4: %s\n", pos4);
printf("pos5: %s\n", pos5);
printf("pos6: %s\n", pos6);
printf("pos7: %s\n", pos7);
printf("pos8: %s\n", pos8);
printf("pos9: %s\n", pos9);
printf("pos10: %s\n", pos10);
printf("pos1: %s\n", pos1 ? pos1 : "null");
printf("pos2: %s\n", pos2 ? pos2 : "null");
printf("pos3: %s\n", pos3 ? pos3 : "null");
printf("pos4: %s\n", pos4 ? pos4 : "null");
printf("pos5: %s\n", pos5 ? pos5 : "null");
printf("pos6: %s\n", pos6 ? pos6 : "null");
printf("pos7: %s\n", pos7 ? pos7 : "null");
printf("pos8: %s\n", pos8 ? pos8 : "null");
printf("pos9: %s\n", pos9 ? pos9 : "null");
printf("pos10: %s\n", pos10 ? pos10 : "null");
printf("aye: %ld\n", aye);
printf("yyy: %s\n", yyy ? "true" : "false");
printf("k: %.4f\n", k);
printf("l: %s\n", l);
printf("l: %s\n", l ? l : "null");
printf("out: %s\n", out ? "true" : "false");
printf("ede: %s\n", ede ? "true" : "false");
printf("vvv: %s\n", vvv);
printf("vvv: %s\n", vvv ? vvv : "null");
printf("ssss: %s\n", ssss ? "true" : "false");
printf("zzzz: %s\n", zzzz ? "true" : "false");
printf("ccc: %s\n", ccc);
printf("ccc: %s\n", ccc ? ccc : "null");
printf("bbb: %ld\n", bbb);
printf("ii: %s\n", ii);
printf("ii: %s\n", ii ? ii : "null");
printf("qqqqsdgvs: %s\n", qqqqsdgvs ? "true" : "false");
printf("y: %s\n", y ? "true" : "false");
printf("r: %s\n", r ? "true" : "false");
Expand Down
6 changes: 3 additions & 3 deletions tests/test_data/normalize_names/expected_c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ int main(int argc, char *argv[])
printf("test_1: %ld\n", test_1);
printf("test___2: %ld\n", test___2);
printf("j__j: %s\n", j__j ? "true" : "false");
printf("lp__l: %s\n", lp__l);
printf("pos_2: %s\n", pos_2);
printf("pos___3: %s\n", pos___3);
printf("lp__l: %s\n", lp__l ? lp__l : "null");
printf("pos_2: %s\n", pos_2 ? pos_2 : "null");
printf("pos___3: %s\n", pos___3 ? pos___3 : "null");

return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/test_data/options_only/expected_c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ int main(int argc, char *argv[])

printf("a: %ld\n", a);
printf("bbb: %.4f\n", bbb);
printf("tttt: %s\n", tttt);
printf("j: %s\n", j);
printf("k: %s\n", k);
printf("tttt: %s\n", tttt ? tttt : "null");
printf("j: %s\n", j ? j : "null");
printf("k: %s\n", k ? k : "null");

return 0;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/test_data/positional_only/expected_c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ int main(int argc, char *argv[])
return ret;
}

printf("pos1: %s\n", pos1);
printf("pos2: %s\n", pos2);
printf("pos3: %s\n", pos3);
printf("pos4: %s\n", pos4);
printf("pos5: %s\n", pos5);
printf("pos6: %s\n", pos6);
printf("pos1: %s\n", pos1 ? pos1 : "null");
printf("pos2: %s\n", pos2 ? pos2 : "null");
printf("pos3: %s\n", pos3 ? pos3 : "null");
printf("pos4: %s\n", pos4 ? pos4 : "null");
printf("pos5: %s\n", pos5 ? pos5 : "null");
printf("pos6: %s\n", pos6 ? pos6 : "null");

return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/test_data/positional_values/expected_c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ int main(int argc, char *argv[])
return ret;
}

printf("positional_arg0: %s\n", positional_arg0);
printf("positional_arg0: %s\n", positional_arg0 ? positional_arg0 : "null");
printf("positional_arg1: %ld\n", positional_arg1);
printf("positional_arg2: %.4f\n", positional_arg2);
printf("hello: %s\n", hello);
printf("ra: %s\n", ra);
printf("hello: %s\n", hello ? hello : "null");
printf("ra: %s\n", ra ? ra : "null");

return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/test_data/readme_example/expected_c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ int main(int argc, char *argv[])
return ret;
}

printf("positional_arg1: %s\n", positional_arg1);
printf("positional_arg2: %s\n", positional_arg2);
printf("positional_arg1: %s\n", positional_arg1 ? positional_arg1 : "null");
printf("positional_arg2: %s\n", positional_arg2 ? positional_arg2 : "null");
printf("int_val: %ld\n", int_val);
printf("e: %.4f\n", e);
printf("file: %s\n", file);
printf("otherfile: %s\n", otherfile);
printf("file: %s\n", file ? file : "null");
printf("otherfile: %s\n", otherfile ? otherfile : "null");
printf("a: %s\n", a ? "true" : "false");
printf("b: %s\n", b ? "true" : "false");
printf("c: %s\n", c ? "true" : "false");
Expand Down

0 comments on commit 78285e3

Please sign in to comment.