<html>
<head>
<base href="https://llvm.org/bugs/" />
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW " title="NEW --- - sprintf format with %.2X mis-formats a char with a value of 0xFF" href="https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_bugs_show-5Fbug.cgi-3Fid-3D23798&d=AwMBaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=pF93YEPyB-J_PERP4DUZOJDzFVX5ZQ57vQk33wu0vio&m=7g_S1AC5y3uS-jVtcgHwzTqg0UNLYykJOcD-aGXBpLs&s=Tg1K4IwQfCRtMP-JGp389kC59n5cXmg-FbDv0m8QZyI&e=">23798</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>sprintf format with %.2X mis-formats a char with a value of 0xFF
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>-New Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>james.baker@bullochtech.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvmbugs@cs.uiuc.edu
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=14451" name="attach_14451" title="C++ test file.">attachment 14451</a> <a href="attachment.cgi?id=14451&action=edit" title="C++ test file.">[details]</a></span>
C++ test file.
sprintf(szDest, "%.2X", szIn[i]) outputs "FFFFFFFF" even when szIn is a char
array. It should output "FF".
Here is code to show the problem:
#include <string.h>
#include <stdio.h>
void succeeds()
{
char data[] = "ABCD2345678";
char dest[128];
char temp[3];
memset(dest, 0, sizeof(dest));
for(int i = 0; i < strlen(data); i++){
sprintf(temp, "%.2X", data[i]);
printf("Part: %s\n", temp);
strcat(dest, temp);
}
printf("Result: %s\n", dest);
}
void fails()
{
char data[] = "\xFF\xFF\xFF\xFF""2345678";
char dest[128];
char temp[3];
memset(dest, 0, sizeof(dest));
for(int i = 0; i < strlen(data); i++){
sprintf(temp, "%.2X", data[i]);
printf("Part: %s\n", temp);
strcat(dest, temp);
}
printf("Result: %s\n", dest);
}
int main(int argc, char ** argv)
{
succeeds();
fails();
return 0;
}
-------END CODE--------
This outputs like this in clang++ (Based on 3.6.0) on the Mac:
Part: 41
Part: 42
Part: 43
Part: 44
Part: 32
Part: 33
Part: 34
Part: 35
Part: 36
Part: 37
Part: 38
Result: 4142434432333435363738
Part: FFFFFFFF
Part: FFFFFFFF
Part: FFFFFFFF
Part: FFFFFFFF
Part: 32
Part: 33
Part: 34
Part: 35
Part: 36
Part: 37
Part: 38
Result: FFFFFFFFFFFFF32333435363738
And it outputs this on Windows (trunk as of June 5 2015) until it crashes:
Part: 41
Part: 42
Part: 43
Part: 44
Part: 32
Part: 33
Part: 34
Part: 35
Part: 36
Part: 37
Part: 38
Result: 4142434432333435363738
Part: FFFFFFFF <--- crashes at this point
On the Raspberry Pi (Ubuntu Mate) Clang++ version 3.6.0:
Part: 41
Part: 42
Part: 43
Part: 44
Part: 32
Part: 33
Part: 34
Part: 35
Part: 36
Part: 37
Part: 38
Result: 4142434432333435363738
Part: FF
Part: FF
Part: FF
Part: FF
Part: 32
Part: 33
Part: 34
Part: 35
Part: 36
Part: 37
Part: 38
Result: FFFFFFFF32333435363738</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>