Verification Controller problems

0

The problem is:

Memory is never released; potential leak of memory pointed to by 'p'

In Code it occurs on the return of this single line method:

- (NSString *)encodeBase64:(const uint8_t *)input length:(NSInteger)length
{
    return [NSString stringWithUTF8String:base64_encode(input, (size_t)length)];
}

base64_encode function:

char* base64_encode(const void* buf, size_t size)
{
    static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    char* str = (char*) malloc((size+3)*4/3 + 1);

    char* p = str;
    unsigned char* q = (unsigned char*) buf;
    size_t i = 0;

    while(i < size) {
        int c = q[i++];
        c *= 256;
        if (i < size) c += q[i];
        i++;

        c *= 256;
        if (i < size) c += q[i];
        i++;

        *p++ = base64[(c & 0x00fc0000) >> 18];
        *p++ = base64[(c & 0x0003f000) >> 12];

        if (i > size + 1)
            *p++ = '=';
        else
            *p++ = base64[(c & 0x00000fc0) >> 6];

        if (i > size)
            *p++ = '=';
        else
            *p++ = base64[c & 0x0000003f];
    }

    *p = 0;

    return str;
}
iphone
ios
asked on Stack Overflow Feb 12, 2013 by h4cky • edited Feb 12, 2013 by Tomas Camin

2 Answers

2

You should free(str) after converting the char * to NSString:

- (NSString *)encodeBase64:(const uint8_t *)input length:(NSInteger)length
{
    char *base64 = base64_encode(input, length);
    NSString *ret = [NSString stringWithUTF8String:base64];
    free(base64);
    return ret;
}
answered on Stack Overflow Feb 12, 2013 by Tomas Camin
0

the memory allocated with the malloc function has to be released with free, you has to change the line

*p = 0;

to

free(p);
answered on Stack Overflow Feb 12, 2013 by tkanzakic

User contributions licensed under CC BY-SA 3.0