[LLVMbugs] [Bug 23798] New: sprintf format with %.2X mis-formats a char with a value of 0xFF

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Tue Jun 9 09:26:25 PDT 2015


https://llvm.org/bugs/show_bug.cgi?id=23798

            Bug ID: 23798
           Summary: sprintf format with %.2X mis-formats a char with a
                    value of 0xFF
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: james.baker at bullochtech.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Created attachment 14451
  --> https://llvm.org/bugs/attachment.cgi?id=14451&action=edit
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

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150609/520d45e4/attachment.html>


More information about the llvm-bugs mailing list