xpcom/io/nsScriptableInputStream.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsScriptableInputStream.h"
michael@0 7 #include "nsMemory.h"
michael@0 8 #include "nsString.h"
michael@0 9
michael@0 10 NS_IMPL_ISUPPORTS(nsScriptableInputStream, nsIScriptableInputStream)
michael@0 11
michael@0 12 // nsIScriptableInputStream methods
michael@0 13 NS_IMETHODIMP
michael@0 14 nsScriptableInputStream::Close(void) {
michael@0 15 if (!mInputStream) return NS_ERROR_NOT_INITIALIZED;
michael@0 16 return mInputStream->Close();
michael@0 17 }
michael@0 18
michael@0 19 NS_IMETHODIMP
michael@0 20 nsScriptableInputStream::Init(nsIInputStream *aInputStream) {
michael@0 21 if (!aInputStream) return NS_ERROR_NULL_POINTER;
michael@0 22 mInputStream = aInputStream;
michael@0 23 return NS_OK;
michael@0 24 }
michael@0 25
michael@0 26 NS_IMETHODIMP
michael@0 27 nsScriptableInputStream::Available(uint64_t *_retval) {
michael@0 28 if (!mInputStream) return NS_ERROR_NOT_INITIALIZED;
michael@0 29 return mInputStream->Available(_retval);
michael@0 30 }
michael@0 31
michael@0 32 NS_IMETHODIMP
michael@0 33 nsScriptableInputStream::Read(uint32_t aCount, char **_retval) {
michael@0 34 nsresult rv = NS_OK;
michael@0 35 uint64_t count64 = 0;
michael@0 36 char *buffer = nullptr;
michael@0 37
michael@0 38 if (!mInputStream) return NS_ERROR_NOT_INITIALIZED;
michael@0 39
michael@0 40 rv = mInputStream->Available(&count64);
michael@0 41 if (NS_FAILED(rv)) return rv;
michael@0 42
michael@0 43 // bug716556 - Ensure count+1 doesn't overflow
michael@0 44 uint32_t count = XPCOM_MIN((uint32_t)XPCOM_MIN<uint64_t>(count64, aCount), UINT32_MAX - 1);
michael@0 45 buffer = (char*)moz_malloc(count+1); // make room for '\0'
michael@0 46 if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
michael@0 47
michael@0 48 rv = ReadHelper(buffer, count);
michael@0 49 if (NS_FAILED(rv)) {
michael@0 50 nsMemory::Free(buffer);
michael@0 51 return rv;
michael@0 52 }
michael@0 53
michael@0 54 buffer[count] = '\0';
michael@0 55 *_retval = buffer;
michael@0 56 return NS_OK;
michael@0 57 }
michael@0 58
michael@0 59 NS_IMETHODIMP
michael@0 60 nsScriptableInputStream::ReadBytes(uint32_t aCount, nsACString &_retval) {
michael@0 61 if (!mInputStream) {
michael@0 62 return NS_ERROR_NOT_INITIALIZED;
michael@0 63 }
michael@0 64
michael@0 65 _retval.SetLength(aCount);
michael@0 66 if (_retval.Length() != aCount) {
michael@0 67 return NS_ERROR_OUT_OF_MEMORY;
michael@0 68 }
michael@0 69
michael@0 70 char *ptr = _retval.BeginWriting();
michael@0 71 nsresult rv = ReadHelper(ptr, aCount);
michael@0 72 if (NS_FAILED(rv)) {
michael@0 73 _retval.Truncate();
michael@0 74 }
michael@0 75 return rv;
michael@0 76 }
michael@0 77
michael@0 78 nsresult
michael@0 79 nsScriptableInputStream::ReadHelper(char* aBuffer, uint32_t aCount)
michael@0 80 {
michael@0 81 uint32_t totalBytesRead = 0;
michael@0 82 while (1) {
michael@0 83 uint32_t bytesRead;
michael@0 84 nsresult rv = mInputStream->Read(aBuffer + totalBytesRead,
michael@0 85 aCount - totalBytesRead,
michael@0 86 &bytesRead);
michael@0 87 if (NS_FAILED(rv)) {
michael@0 88 return rv;
michael@0 89 }
michael@0 90
michael@0 91 totalBytesRead += bytesRead;
michael@0 92 if (totalBytesRead == aCount) {
michael@0 93 break;
michael@0 94 }
michael@0 95
michael@0 96 // If we have read zero bytes, we have hit EOF.
michael@0 97 if (bytesRead == 0) {
michael@0 98 return NS_ERROR_FAILURE;
michael@0 99 }
michael@0 100
michael@0 101 }
michael@0 102 return NS_OK;
michael@0 103 }
michael@0 104
michael@0 105 nsresult
michael@0 106 nsScriptableInputStream::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) {
michael@0 107 if (aOuter) return NS_ERROR_NO_AGGREGATION;
michael@0 108
michael@0 109 nsScriptableInputStream *sis = new nsScriptableInputStream();
michael@0 110 if (!sis) return NS_ERROR_OUT_OF_MEMORY;
michael@0 111
michael@0 112 NS_ADDREF(sis);
michael@0 113 nsresult rv = sis->QueryInterface(aIID, aResult);
michael@0 114 NS_RELEASE(sis);
michael@0 115 return rv;
michael@0 116 }

mercurial