Skip to content

Commit 9a06a88

Browse files
committed
added ptr verification in stack_pop() and stack_dumpToStream(); redefined bit stack_status codes as 1<<n;
1 parent 3b99275 commit 9a06a88

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

gstack-header.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <string.h>
3434
#include <math.h>
3535

36-
#include <nmmintrin.h> /// for crc32 intrinsic
36+
#include <nmmintrin.h> /// for crc32 intrinsic
3737
#include <inttypes.h>
3838
#define __STDC_FORMAT_MACROS
3939

@@ -105,22 +105,22 @@ typedef unsigned long long STACK_CANARY_TYPE; /// Type for canaries can
105105
typedef int stack_status; /// stack status is a bitset inside an int
106106

107107
enum stack_status_enum { /// ERROR codes for stack
108-
STACK_OK = 0, /// All_is_fine status
109-
110-
STACK_BAD_STRUCT_PTR = 0b00001, /// Bad ptr for stack structure provided
111-
STACK_BAD_DATA_PTR = 0b00010, /// Bad ptr for stack data
112-
STACK_BAD_MEM_ALLOC = 0b00100, /// Error during memory (re)allocation
113-
STACK_INTEGRITY_VIOLATED = 0b01000, /// Stack structure intergrity violated
114-
STACK_DATA_INTEGRITY_VIOLATED = 0b10000, /// Stack data intergrity violated
115-
116-
STACK_LEFT_STRUCT_CANARY_CORRUPT = 0b0001000000, /// Stack canary has been modified
117-
STACK_RIGHT_STRUCT_CANARY_CORRUPT = 0b0010000000, /// could happen if big chank of data
118-
STACK_LEFT_DATA_CANARY_CORRUPT = 0b0100000000, /// has been carelessly filled with data
119-
STACK_RIGHT_DATA_CANARY_CORRUPT = 0b1000000000, /// or if stack data has been writen above it
120-
121-
STACK_BAD_STRUCT_HASH = 0b0010000000000, /// Bad hash of all stack structure filds
122-
STACK_BAD_DATA_HASH = 0b0100000000000, /// Bad hash of all the stack data
123-
STACK_BAD_CAPACITY = 0b1000000000000 /// Stack capacity has been modified and/or is clearly incorrect
108+
STACK_OK = 0, /// All_is_fine status
109+
110+
STACK_BAD_STRUCT_PTR = 1<<0, /// Bad ptr for stack structure provided
111+
STACK_BAD_DATA_PTR = 1<<1, /// Bad ptr for stack data
112+
STACK_BAD_MEM_ALLOC = 1<<2, /// Error during memory (re)allocation
113+
STACK_INTEGRITY_VIOLATED = 1<<3, /// Stack structure intergrity violated
114+
STACK_DATA_INTEGRITY_VIOLATED = 1<<4, /// Stack data intergrity violated
115+
116+
STACK_LEFT_STRUCT_CANARY_CORRUPT = 1<<7, /// Stack canary has been modified
117+
STACK_RIGHT_STRUCT_CANARY_CORRUPT = 1<<8, /// could happen if big chank of data
118+
STACK_LEFT_DATA_CANARY_CORRUPT = 1<<9, /// has been carelessly filled with data
119+
STACK_RIGHT_DATA_CANARY_CORRUPT = 1<<10, /// or if stack data has been writen above it
120+
121+
STACK_BAD_STRUCT_HASH = 1<<13, /// Bad hash of all stack structure filds
122+
STACK_BAD_DATA_HASH = 1<<14, /// Bad hash of all the stack data
123+
STACK_BAD_CAPACITY = 1<<15 /// Stack capacity has been modified and/or is clearly incorrect
124124
};
125125

126126

@@ -192,9 +192,8 @@ static bool ptrValid(const void* ptr);
192192
#endif
193193

194194

195-
/// macro for accessing left data canary wrapper from inside of a func with defined `this_`
195+
/// macros for accessing Left and Right data canary wrapper from inside of a func with defined `this_`
196196
#define LEFT_CANARY_WRAPPER (this_->dataWrapper)
197-
/// macro for accessing right data canary wrapper from inside of a func with defined `this_`
198197
#define RIGHT_CANARY_WRAPPER ((STACK_CANARY_TYPE*)((char*)this_->dataWrapper + STACK_CANARY_WRAPPER_LEN * sizeof(STACK_CANARY_TYPE) + this_->capacity * sizeof(STACK_TYPE)))
199198

200199

@@ -378,7 +377,7 @@ static stack_status stack_push(stack *this_, STACK_TYPE item);
378377
* @fn static stack_status stack_pop (stack *this_, STACK_TYPE item)
379378
* @brief pops last elem from stack
380379
* @param this_ pointer to stack
381-
* @param item pointer to var to write to
380+
* @param item pointer to var to write to or NULL if value should be discarded
382381
* @return bitset of stack status
383382
*/
384383
static stack_status stack_pop (stack *this_, STACK_TYPE* item);

gstack.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -293,20 +293,22 @@ static stack_status stack_pop(stack *this_, STACK_TYPE* item)
293293
}
294294

295295
this_->len -= 1;
296-
*item = this_->data[this_->len];
297-
298-
#ifdef STACK_USE_POISON //TODO do smth else?
299-
if (stack_isPoisoned(item)) {
300-
STACK_LOG_TO_STREAM(this_, this_->logStream, "WARNING: accessed uninitilized element!");
301-
}
302-
memset((char*)(&this_->data[this_->len]), STACK_ELEM_POISON, sizeof(STACK_TYPE));
303-
#endif
304296

305-
#ifdef STACK_USE_CANARY
306-
// if (stack_isCanaryVal(item)) //TODO think if it is possible to do this check properly
307-
// STACK_LOG_TO_STREAM(this_, this_->logStream, "WARNING accessed cannary wrapper element!");
308-
#endif
309-
297+
if (ptrValid(item)) {
298+
*item = this_->data[this_->len];
299+
#ifdef STACK_USE_POISON //TODO do smth else?
300+
if (stack_isPoisoned(item)) {
301+
STACK_LOG_TO_STREAM(this_, this_->logStream, "WARNING: accessed uninitilized element!");
302+
}
303+
memset((char*)(&this_->data[this_->len]), STACK_ELEM_POISON, sizeof(STACK_TYPE));
304+
#endif
305+
306+
#ifdef STACK_USE_CANARY
307+
// if (stack_isCanaryVal(item)) //TODO think if it is possible to do this check properly
308+
// STACK_LOG_TO_STREAM(this_, this_->logStream, "WARNING accessed cannary wrapper element!");
309+
#endif
310+
}
311+
310312
#ifdef AUTO_SHRINK
311313
size_t newCapacity = stack_shrinkageFactorCalc(this_->capacity);
312314

@@ -324,7 +326,6 @@ static stack_status stack_pop(stack *this_, STACK_TYPE* item)
324326
this_->structHash = stack_calculateStructHash(this_);
325327
#endif
326328

327-
328329
return STACK_HEALTH_CHECK(this_);
329330
}
330331

@@ -334,7 +335,7 @@ static stack_status stack_reallocate(stack *this_, const size_t newCapacity)
334335
STACK_HEALTH_CHECK(this_);
335336

336337
#ifdef STACK_USE_POISON
337-
if (newCapacity < this_->capacity)
338+
if (newCapacity < this_->capacity)
338339
{
339340
memset((char*)(this_->data + newCapacity), STACK_FREED_POISON, (this_->capacity - newCapacity) * sizeof(STACK_TYPE));
340341
}
@@ -383,6 +384,11 @@ static stack_status stack_reallocate(stack *this_, const size_t newCapacity)
383384
static stack_status stack_dumpToStream(const stack *this_, FILE *out)
384385
{
385386
STACK_PTR_VALIDATE(this_);
387+
if (!ptrValid(out)) {
388+
fprintf(stderr, "WARNING: Bad log stream provided, outputing to stderr.\n");
389+
out = stderr;
390+
}
391+
386392

387393
fprintf(out, "%s\n", STACK_LOG_DELIM);
388394
fprintf(out, "| Stack [%p] :\n", this_);

0 commit comments

Comments
 (0)