vfs: check userland buffers before reading them.
[haiku.git] / src / servers / app / drawing / MallocBuffer.cpp
blobf72ef26bd9c092232ff28d58c4aec7e92b0eaa7e
1 // MallocBuffer.h
3 #include <malloc.h>
5 #include "MallocBuffer.h"
7 // TODO: maybe this class could be more flexible by taking
8 // a color_space argument in the constructor
9 // the hardcoded width * 4 (because that's how it's used now anyways)
10 // could be avoided, but I'm in a hurry... :-)
12 // constructor
13 MallocBuffer::MallocBuffer(uint32 width,
14 uint32 height)
15 : fBuffer(NULL),
16 fWidth(width),
17 fHeight(height)
19 if (fWidth > 0 && fHeight > 0) {
20 fBuffer = malloc((fWidth * 4) * fHeight);
24 // destructor
25 MallocBuffer::~MallocBuffer()
27 if (fBuffer)
28 free(fBuffer);
31 // InitCheck
32 status_t
33 MallocBuffer::InitCheck() const
35 return fBuffer ? B_OK : B_NO_MEMORY;
38 // ColorSpace
39 color_space
40 MallocBuffer::ColorSpace() const
42 return B_RGBA32;
45 // Bits
46 void*
47 MallocBuffer::Bits() const
49 if (InitCheck() >= B_OK)
50 return fBuffer;
51 return NULL;
54 // BytesPerRow
55 uint32
56 MallocBuffer::BytesPerRow() const
58 if (InitCheck() >= B_OK)
59 return fWidth * 4;
60 return 0;
63 // Width
64 uint32
65 MallocBuffer::Width() const
67 if (InitCheck() >= B_OK)
68 return fWidth;
69 return 0;
72 // Height
73 uint32
74 MallocBuffer::Height() const
76 if (InitCheck() >= B_OK)
77 return fHeight;
78 return 0;