Ignore:
Timestamp:
Nov 16, 2010, 10:04:52 AM (15 years ago)
Author:
Darin Adler
Message:

2010-11-15 Darin Adler <Darin Adler>

Reviewed by Sam Weinig.

Harden additional string functions against large lengths
https://bugs.webkit.org/show_bug.cgi?id=49574

  • wtf/text/CString.cpp: (WTF::CString::init): Check for length that is too large for CString. (WTF::CString::newUninitialized): Ditto. (WTF::CString::copyBufferIfNeeded): Fix types so the length stays in a size_t.
  • wtf/text/WTFString.cpp: (WTF::String::append): Check for length that is too large.

2010-11-15 Darin Adler <Darin Adler>

Reviewed by Sam Weinig.

Harden additional string functions against large lengths
https://bugs.webkit.org/show_bug.cgi?id=49574

  • platform/text/TextCodecUTF16.cpp: (WebCore::TextCodecUTF16::encode): Check for length that is too large for size_t.
  • platform/text/TextStream.cpp: (WebCore::TextStream::operator<<): Check for length that is too large for size_t.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/text/WTFString.cpp

    r69414 r72114  
    11/*
    22 * (C) 1999 Lars Knoll ([email protected])
    3  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
     3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
    44 * Copyright (C) 2007-2009 Torch Mobile, Inc.
    55 *
     
    2323#include "WTFString.h"
    2424
    25 #include <limits>
    2625#include <stdarg.h>
    2726#include <wtf/ASCIICType.h>
     
    3332#include <wtf/unicode/Unicode.h>
    3433
     34using namespace std;
     35
    3536namespace WTF {
    3637
     
    5354        len++;
    5455
    55     if (len > std::numeric_limits<unsigned>::max())
     56    if (len > numeric_limits<unsigned>::max())
    5657        CRASH();
    5758   
     
    8384        if (m_impl) {
    8485            UChar* data;
    85             RefPtr<StringImpl> newImpl =
    86                 StringImpl::createUninitialized(m_impl->length() + str.length(), data);
     86            if (str.length() > numeric_limits<unsigned>::max() - m_impl->length())
     87                CRASH();
     88            RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(m_impl->length() + str.length(), data);
    8789            memcpy(data, m_impl->characters(), m_impl->length() * sizeof(UChar));
    8890            memcpy(data + m_impl->length(), str.characters(), str.length() * sizeof(UChar));
     
    101103    if (m_impl) {
    102104        UChar* data;
    103         RefPtr<StringImpl> newImpl =
    104             StringImpl::createUninitialized(m_impl->length() + 1, data);
     105        if (m_impl->length() >= numeric_limits<unsigned>::max())
     106            CRASH();
     107        RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(m_impl->length() + 1, data);
    105108        memcpy(data, m_impl->characters(), m_impl->length() * sizeof(UChar));
    106109        data[m_impl->length()] = c;
     
    118121    if (m_impl) {
    119122        UChar* data;
    120         RefPtr<StringImpl> newImpl =
    121             StringImpl::createUninitialized(m_impl->length() + 1, data);
     123        if (m_impl->length() >= numeric_limits<unsigned>::max())
     124            CRASH();
     125        RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(m_impl->length() + 1, data);
    122126        memcpy(data, m_impl->characters(), m_impl->length() * sizeof(UChar));
    123127        data[m_impl->length()] = c;
     
    179183    ASSERT(charactersToAppend);
    180184    UChar* data;
    181     if (lengthToAppend > std::numeric_limits<unsigned>::max() - length())
     185    if (lengthToAppend > numeric_limits<unsigned>::max() - length())
    182186        CRASH();
    183     RefPtr<StringImpl> newImpl =
    184         StringImpl::createUninitialized(length() + lengthToAppend, data);
     187    RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(length() + lengthToAppend, data);
    185188    memcpy(data, characters(), length() * sizeof(UChar));
    186189    memcpy(data + length(), charactersToAppend, lengthToAppend * sizeof(UChar));
     
    202205    ASSERT(charactersToInsert);
    203206    UChar* data;
    204     if (lengthToInsert > std::numeric_limits<unsigned>::max() - length())
     207    if (lengthToInsert > numeric_limits<unsigned>::max() - length())
    205208        CRASH();
    206     RefPtr<StringImpl> newImpl =
    207       StringImpl::createUninitialized(length() + lengthToInsert, data);
     209    RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(length() + lengthToInsert, data);
    208210    memcpy(data, characters(), position * sizeof(UChar));
    209211    memcpy(data + position, charactersToInsert, lengthToInsert * sizeof(UChar));
     
    238240        lengthToRemove = length() - position;
    239241    UChar* data;
    240     RefPtr<StringImpl> newImpl =
    241         StringImpl::createUninitialized(length() - lengthToRemove, data);
     242    RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(length() - lengthToRemove, data);
    242243    memcpy(data, characters(), position * sizeof(UChar));
    243244    memcpy(data + position, characters() + position + lengthToRemove,
     
    726727String String::fromUTF8(const char* stringStart, size_t length)
    727728{
    728     if (length > std::numeric_limits<unsigned>::max())
     729    if (length > numeric_limits<unsigned>::max())
    729730        CRASH();
    730731
     
    788789static inline IntegralType toIntegralType(const UChar* data, size_t length, bool* ok, int base)
    789790{
    790     static const IntegralType integralMax = std::numeric_limits<IntegralType>::max();
    791     static const bool isSigned = std::numeric_limits<IntegralType>::is_signed;
     791    static const IntegralType integralMax = numeric_limits<IntegralType>::max();
     792    static const bool isSigned = numeric_limits<IntegralType>::is_signed;
    792793    const IntegralType maxMultiplier = integralMax / base;
    793794
Note: See TracChangeset for help on using the changeset viewer.