VirtualBox

Changeset 40058 in vbox


Ignore:
Timestamp:
Feb 10, 2012 1:10:26 PM (13 years ago)
Author:
vboxsync
Message:

RTBase64: fix trailing newline suppression in RTBase64EncodedLength, plus testcase

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/base64.cpp

    r33540 r40058  
    424424        cch /= 6;
    425425
    426         cch += (cch / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
    427         cch -= (cch % RTBASE64_LINE_LEN) == 0;
     426        cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
    428427        return cch;
    429428    }
     
    434433    cch /= 6;
    435434
    436     cch += (cch / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
    437     cch -= (cch % RTBASE64_LINE_LEN) == 0;
     435    cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
    438436    return cch;
    439437}
  • trunk/src/VBox/Runtime/testcase/tstRTBase64.cpp

    r28800 r40058  
    55
    66/*
    7  * Copyright (C) 2009 Oracle Corporation
     7 * Copyright (C) 2009-2012 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    173173
    174174    /*
     175     * Test for buffer overruns.
     176     */
     177    static uint8_t s_abData4[32768];
     178    for (size_t i = 0; i < sizeof(s_abData4); i++)
     179        s_abData4[i] = i % 256;
     180    for (size_t cbSrc = 1; cbSrc <= sizeof(s_abData4); cbSrc++)
     181    {
     182        RTTestSubF(hTest, "Test 3-%zu", cbSrc);
     183        char szEnc[49152];
     184        memset(szEnc, '\0', sizeof(szEnc));
     185        size_t cchEnc = RTBase64EncodedLength(cbSrc);
     186        if (cchEnc >= sizeof(szEnc))
     187            RTTestIFailed("RTBase64EncodedLength returned %zu bytes, too big\n", cchEnc);
     188        size_t cchOut = 0;
     189        rc = RTBase64Encode(s_abData4, cbSrc, szEnc, cchEnc, &cchOut);
     190        if (rc != VERR_BUFFER_OVERFLOW)
     191            RTTestIFailed("RTBase64Encode has no buffer overflow with too small buffer -> %Rrc\n", rc);
     192        rc = RTBase64Encode(s_abData4, cbSrc, szEnc, cchEnc + 1, &cchOut);
     193        if (RT_FAILURE(rc))
     194            RTTestIFailed("RTBase64Encode -> %Rrc\n", rc);
     195        if (cchOut != cchEnc)
     196            RTTestIFailed("RTBase64EncodedLength returned %zu bytes, expected %zu.\n",
     197                          cchEnc, cchOut);
     198        if (szEnc[cchOut + 1] != '\0')
     199            RTTestIFailed("RTBase64Encode returned string which is not zero terminated\n");
     200        if (strlen(szEnc) != cchOut)
     201            RTTestIFailed("RTBase64Encode returned incorrect string, length %lu\n", cchOut);
     202    }
     203
     204    /*
    175205     * Finally, a more extensive test.
    176206     */
    177     RTTestSub(hTest, "Test 3");
     207    RTTestSub(hTest, "Test 4");
    178208    static uint8_t s_abData3[12*256];
    179209    for (unsigned i = 0; i < 256; i++)
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette