VirtualBox

Changeset 75045 in vbox


Ignore:
Timestamp:
Oct 24, 2018 2:46:53 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
126088
Message:

IPRT/RTFileReadAll*: Added RTFILE_RDALL_F_FAIL_ON_MAX_SIZE. bugref:9232

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/file.h

    r69977 r75045  
    11311131#define RTFILE_RDALL_O_DENY_NOT_DELETE      RTFILE_O_DENY_NOT_DELETE
    11321132#define RTFILE_RDALL_O_DENY_MASK            RTFILE_O_DENY_MASK
     1133/** Fail with VERR_OUT_OF_RANGE if the file size exceeds the specified maximum
     1134 * size.  The default behavior is to cap the size at cbMax. */
     1135#define RTFILE_RDALL_F_FAIL_ON_MAX_SIZE     RT_BIT_32(30)
    11331136/** Add a trailing zero byte to facilitate reading text files. */
    11341137#define RTFILE_RDALL_F_TRAILING_ZERO_BYTE   RT_BIT_32(31)
    11351138/** Mask of valid flags. */
    1136 #define RTFILE_RDALL_VALID_MASK             (RTFILE_RDALL_O_DENY_MASK | UINT32_C(0x80000000))
     1139#define RTFILE_RDALL_VALID_MASK             (RTFILE_RDALL_O_DENY_MASK | UINT32_C(0xc0000000))
    11371140/** @} */
    11381141
  • trunk/src/VBox/Runtime/generic/RTFileReadAllByHandleEx-generic.cpp

    r69111 r75045  
    5353         */
    5454        RTFOFF cbFile;
    55         rc = RTFileSeek(File, 0,RTFILE_SEEK_END, (uint64_t *)&cbFile);
     55        AssertCompile(sizeof(cbFile) == sizeof(uint64_t));
     56        rc = RTFileSeek(File, 0, RTFILE_SEEK_END, (uint64_t *)&cbFile);
    5657        if (RT_SUCCESS(rc))
    5758        {
    5859            RTFOFF cbAllocFile = cbFile > off ? cbFile - off : 0;
    59             if (cbAllocFile > cbMax)
     60            if (cbAllocFile <= cbMax)
     61            { /* likely */ }
     62            else if (!(fFlags & RTFILE_RDALL_F_FAIL_ON_MAX_SIZE))
    6063                cbAllocFile = cbMax;
    61             size_t cbAllocMem = (size_t)cbAllocFile;
    62             if ((RTFOFF)cbAllocMem == cbAllocFile)
     64            else
     65                rc = VERR_OUT_OF_RANGE;
     66            if (RT_SUCCESS(rc))
    6367            {
    64                 /*
    65                  * Try allocate the required memory and initialize the header (hardcoded fun).
    66                  */
    67                 void *pvHdr = RTMemAlloc(cbAllocMem + 32 + (fFlags & RTFILE_RDALL_F_TRAILING_ZERO_BYTE ? 1 : 0));
    68                 if (pvHdr)
     68                size_t cbAllocMem = (size_t)cbAllocFile;
     69                if ((RTFOFF)cbAllocMem == cbAllocFile)
    6970                {
    70                     memset(pvHdr, 0xff, 32);
    71                     *(size_t *)pvHdr = cbAllocMem;
     71                    /*
     72                     * Try allocate the required memory and initialize the header (hardcoded fun).
     73                     */
     74                    void *pvHdr = RTMemAlloc(cbAllocMem + 32 + (fFlags & RTFILE_RDALL_F_TRAILING_ZERO_BYTE ? 1 : 0));
     75                    if (pvHdr)
     76                    {
     77                        memset(pvHdr, 0xff, 32);
     78                        *(size_t *)pvHdr = cbAllocMem;
    7279
    73                     /*
    74                      * Seek and read.
    75                      */
    76                     rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
    77                     if (RT_SUCCESS(rc))
    78                     {
    79                         void *pvFile = (uint8_t *)pvHdr + 32;
    80                         rc = RTFileRead(File, pvFile, cbAllocMem, NULL);
     80                        /*
     81                         * Seek and read.
     82                         */
     83                        rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
    8184                        if (RT_SUCCESS(rc))
    8285                        {
    83                             if (fFlags & RTFILE_RDALL_F_TRAILING_ZERO_BYTE)
    84                                 ((uint8_t *)pvFile)[cbAllocFile] = '\0';
     86                            void *pvFile = (uint8_t *)pvHdr + 32;
     87                            rc = RTFileRead(File, pvFile, cbAllocMem, NULL);
     88                            if (RT_SUCCESS(rc))
     89                            {
     90                                if (fFlags & RTFILE_RDALL_F_TRAILING_ZERO_BYTE)
     91                                    ((uint8_t *)pvFile)[cbAllocFile] = '\0';
    8592
    86                             /*
    87                              * Success - fill in the return values.
    88                              */
    89                             *ppvFile = pvFile;
    90                             *pcbFile = cbAllocMem;
     93                                /*
     94                                 * Success - fill in the return values.
     95                                 */
     96                                *ppvFile = pvFile;
     97                                *pcbFile = cbAllocMem;
     98                            }
    9199                        }
     100
     101                        if (RT_FAILURE(rc))
     102                            RTMemFree(pvHdr);
    92103                    }
    93 
    94                     if (RT_FAILURE(rc))
    95                         RTMemFree(pvHdr);
     104                    else
     105                        rc = VERR_NO_MEMORY;
    96106                }
    97107                else
    98                     rc = VERR_NO_MEMORY;
     108                    rc = VERR_TOO_MUCH_DATA;
    99109            }
    100             else
    101                 rc = VERR_TOO_MUCH_DATA;
    102110        }
    103111        /* restore the position. */
Note: See TracChangeset for help on using the changeset viewer.

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