Re: strange behaviour
- From: Wolfgang Draxinger <wdraxinger@xxxxxxxxxxxxxxxx>
- Date: Tue, 13 Jan 2009 16:49:34 +0100
tfelb wrote:
Thanks! This code is in /pre/ stadium so i know it doesn't have
any security checks
Epic failure!
Security is difficult to add later in existing systems. Always, I
repeat, always design interfaces in a
but i think its better to use a pointer
array as an argument instead of a double pointer.
You mean something like
char *s2[]
? Technically it behaves exactly like char **s2, though at
compilation stage the types subtely differ. However this is
something of interest for compiler writers, the average coder
should know, that the "pointer to pointer" notation is almost
the same as the "array of pointers" notation and is identical,
when it comes to machine language.
Now it works.
It works in if well formulated data is input. A much safer
interface would be:
void join(
unsigned int const dst_buffer_length,
char * const dst_buffer,
char const * const delimiter_string,
unsigned int const src_string_count
char const * const * const src_strings,
);
A even more safer style would completely abandon the use
of "naked" arrays and instead encapsulate every array operation
within bounds checking helper functions. Have a look at Felix
von Leitner's libowfat library: It provides such an array
abstraction - together with things like overflow safe integer
multiplication and other neat stuff.
<http://www.fefe.de/libowfat/>
Just to give you an idea how it works with arrays, this is an
excerpt of "array.h" of libowfat:
#ifndef ARRAY_H
#define ARRAY_H
#include "uint64.h"
typedef struct {
char* p;
int64 allocated; /* in bytes */
uint64 initialized; /* in bytes */
/* p and allocated nonzero: array is allocated */
/* p and allocated zero: array is unallocated */
/* p zero and allocated < 0: array is failed */
} array;
void* array_allocate(array* x,uint64 membersize,int64 pos);
void* array_get(array* x,uint64 membersize,int64 pos);
void* array_start(const array* const x);
int64 array_length(const array* const x,uint64 membersize);
int64 array_bytes(const array* const x);
void array_truncate(array* x,uint64 membersize,int64 len);
void array_trunc(array* x);
void array_reset(array* x);
void array_fail(array* x);
/* ... */
uint64.h is the header to overflow safe multiplication code and
associated types (it wont resort to bignums, if a multiplication
overflows, but such is detected and triggers error handling).
Thow only "drawback" of libowfat is, that it's GPL licenced, so
you can't use it in a closed source project. But you're still
free to have a look at it's code and get inspiration from it.
Wolfgang Draxinger
--
E-Mail address works, Jabber: hexarith@xxxxxxxxxx, ICQ: 134682867
.
- Follow-Ups:
- Re: strange behaviour
- From: Keith Thompson
- Re: strange behaviour
- References:
- strange behaviour
- From: tfelb
- Re: strange behaviour
- From: Wolfgang Draxinger
- Re: strange behaviour
- From: tfelb
- strange behaviour
- Prev by Date: Re: People's time being wasted by 'Han from China'
- Next by Date: Re: People's time being wasted by 'Han from China'
- Previous by thread: Re: strange behaviour
- Next by thread: Re: strange behaviour
- Index(es):
Relevant Pages
|