Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions changelog/chkprintf.md → changelog/chkformat.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Validate printf arguments against format specifiers
# Validate printf and scanf (variants too) arguments against format specifiers

Follows the C99 specification 7.19.6.1 for printf.
Follows the C99 specification 7.19.6.1 for printf and 7.19.6.2 for scanf.

Takes a generous, rather than strict, view of compatiblity.
For printf, it takes a generous, rather than strict, view of compatiblity.
For example, an unsigned value can be formatted with a signed specifier.

For scanf, it takes a strict view of compatiblity.

Diagnosed incompatibilities are:

1. incompatible sizes which will cause argument misalignment
Expand All @@ -20,7 +22,7 @@ Per the C Standard, extra arguments are ignored.

No attempt is made to fix the arguments or the format string.

In order to use non-Standard printf formats, an easy workaround is:
In order to use non-Standard printf/scanf formats, an easy workaround is:

```
printf("%k\n", value); // error: non-Standard format k
Expand All @@ -36,15 +38,14 @@ Most of the errors detected are portability issues. For instance,
string s;
printf("%.*s\n", s.length, s.ptr);
printf("%d\n", s.sizeof);
long i;
printf("%ld\n", i);
ulong u;
scanf("%lld%*c\n", &u);
```
should be replaced with:
```
string s;
printf("%.*s\n", cast(int) s.length, s.ptr);
printf("%zd\n", s.sizeof);
long i;
printf("%lld\n", i);
ulong u;
scanf("%llu%*c\n", &u);
```

4 changes: 2 additions & 2 deletions src/build.d
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ auto sourceFiles()
"),
frontend: fileArray(env["D"], "
access.d aggregate.d aliasthis.d apply.d argtypes.d argtypes_sysv_x64.d arrayop.d
arraytypes.d ast_node.d astcodegen.d attrib.d blockexit.d builtin.d canthrow.d
arraytypes.d ast_node.d astcodegen.d attrib.d blockexit.d builtin.d canthrow.d chkformat.d
cli.d clone.d compiler.d complex.d cond.d constfold.d cppmangle.d cppmanglewin.d ctfeexpr.d
ctorflow.d dcast.d dclass.d declaration.d delegatize.d denum.d dimport.d dinifile.d
dinterpret.d dmacro.d dmangle.d dmodule.d doc.d dscope.d dstruct.d dsymbol.d dsymbolsem.d
Expand All @@ -1168,7 +1168,7 @@ auto sourceFiles()
parse.d parsetimevisitor.d permissivevisitor.d printast.d safe.d sapply.d scanelf.d scanmach.d
scanmscoff.d scanomf.d semantic2.d semantic3.d sideeffect.d statement.d statement_rewrite_walker.d
statementsem.d staticassert.d staticcond.d target.d templateparamsem.d traits.d
transitivevisitor.d typesem.d typinf.d utils.d visitor.d vsoptions.d foreachvar.d chkprintf.d
transitivevisitor.d typesem.d typinf.d utils.d visitor.d vsoptions.d foreachvar.d
"),
backendHeaders: fileArray(env["C"], "
cc.d cdef.d cgcv.d code.d cv4.d dt.d el.d global.d
Expand Down
Loading