Skip to content
This repository has been archived by the owner on Apr 14, 2020. It is now read-only.

libmtp crashes if locale is set to a country that uses a comma for decimal separator. #33

Open
codestation opened this issue Aug 9, 2013 · 0 comments

Comments

@codestation
Copy link

The function VitaMTP_Data_Metadata_To_XML formats a float number to a string then it tries to replace the dot in the decimal by a comma. If the application who loads libmtp changes the locale to one where it uses a comma for decimal separator then the library crashes since it doesn't check that the result of strchr returns a valid pointer (there is no dot in the formatted number).

Here is a testcase that reproduces the issue:

#include <locale.h>
#include <stdio.h>

int main()
{
    setlocale(LC_ALL, "de_DE.UTF-8");
    printf("LC_ALL: %s\n", setlocale(LC_ALL, NULL));
    printf("LC_CTYPE: %s\n", setlocale(LC_CTYPE, NULL));
    printf("%.6f\n", 1.0f);

    setlocale(LC_ALL, "C");
    printf("LC_ALL: %s\n", setlocale(LC_ALL, NULL));
    printf("LC_CTYPE: %s\n", setlocale(LC_CTYPE, NULL));
    printf("%.6f\n", 1.0f);
    return 0;
}

The fix is to make a check before trying to change the dot for a comma

if(period)
    *period = ','; // All this to make period a comma, maybe there is an easier way?
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant