Skip to content

Commit 43e035a

Browse files
committed
pgm 22.19, write dos newline as a string rather than 2 chars: this turned up a bug in clang
1 parent fbf6f54 commit 43e035a

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,4 @@ spec/parts-list-io-spec
183183
22/22.16-cp
184184
22/dos2unix
185185
22/unix2dos
186+
weird/compound-literal-string-parameter-clang-bug

22/22.19-convert-line-endings.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,8 @@ static int detect_unix_newline(FILE *fp, bool *is_newline)
109109

110110
static int write_dos_newline(FILE *fp)
111111
{
112-
if (fputc(0x0D, fp) == EOF)
113-
return EOF;
114-
115-
return fputc(0x0A, fp);
112+
static char newline[] = {0x0D, 0x0A};
113+
return fputs(newline, fp);
116114
}
117115

118116
int main(int argc, char *argv[])
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
3+
/*
4+
* $ clang --version
5+
* clang version 3.1 (branches/release_31)
6+
* Target: x86_64-apple-darwin10.8.0
7+
* Thread model: posix
8+
*/
9+
10+
11+
/*
12+
* output without call to printf from within main:
13+
*
14+
* $ weird/compound-literal-string-parameter-clang-bug | xxd
15+
* 0000000: 0d0a 20e7 bf5f ff7f .. .._..
16+
* $ weird/compound-literal-string-parameter-clang-bug | wc
17+
* 1 1 8
18+
*/
19+
20+
/* works fine compiled with i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 */
21+
void write_dos_newline()
22+
{
23+
printf("%s", (char[2]){0x0D, 0x0A});
24+
}
25+
26+
27+
int main(void)
28+
{
29+
/* call from main works as expected */
30+
printf("%s", (char[2]){0x0D, 0x0A});
31+
/* call via function appends 6 bytes of junk */
32+
write_dos_newline();
33+
return 0;
34+
}

0 commit comments

Comments
 (0)