vfs: check userland buffers before reading them.
[haiku.git] / src / bin / debug / strace / Context.cpp
blob9a9eb828bbdb57dec12827d5f09013a335112f4e
1 /*
2 * Copyright 2007, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Hugo Santos <hugosantos@gmail.com>
7 * Ingo Weinhold <bonefish@cs.tu-berlin.de>
8 */
10 #include "Context.h"
12 #include <stdio.h>
13 #include <string.h>
15 string
16 Context::FormatSigned(int64 value, int bytes) const
18 char tmp[32];
20 // decimal
22 if (fDecimal) {
23 snprintf(tmp, sizeof(tmp), "%" B_PRId64, value);
24 return tmp;
27 // hex
29 snprintf(tmp, sizeof(tmp), "0x%" B_PRIx64, value);
31 // Negative numbers are expanded when being converted to int64. Hence
32 // we skip all but the last 2 * bytes hex digits to retain the original
33 // type's width.
34 int len = strlen(tmp);
35 int offset = len - min_c(len, bytes * 2);
37 // use the existing "0x" prefix or prepend it again
38 if (offset <= 2) {
39 offset = 0;
40 } else {
41 tmp[--offset] = 'x';
42 tmp[--offset] = '0';
45 return tmp + offset;
48 string
49 Context::FormatUnsigned(uint64 value) const
51 char tmp[32];
52 snprintf(tmp, sizeof(tmp), fDecimal ? "%" B_PRIu64 : "0x%" B_PRIx64, value);
53 return tmp;
56 string
57 Context::FormatFlags(uint64 value) const
59 char tmp[32];
60 snprintf(tmp, sizeof(tmp), "0x%" B_PRIx64, value);
61 return tmp;
64 string
65 Context::FormatPointer(const void *address) const
67 char buffer[32];
68 sprintf(buffer, "%p", address);
69 return buffer;