Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-other / 2009-05

Re: Microsoft to ban Memcpy() :)

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jonas Maebe
Jonas Maebe wrote 5 lines
On 15 May 2009, at 21:19, ABorka wrote: > Funny... always these problems with C and bounds checkings Please use the fpc-other list for remarks like this.

And of course, the move() procedure in FPC/TP/Delphi has exactly the same problems as memcpy() as far as bounds-unsafety is concerned (it's similar with fillchar/memset, indexbyte/memchr, etc).

Jonas

Reply by email

Vinzent Höfler
to Jonas Maebe

Jonas Maebe :

And of course, the move() procedure in FPC/TP/Delphi has exactly the same problems as memcpy() as far as bounds-unsafety is concerned (it's similar with fillchar/memset, indexbyte/memchr, etc).

True. But in contrast to C an average programmer doesn't need it very often. In Pascal there are easier ways to assign arrays to each other than doing a raw byte copy. ;)

But I still fail to see the advantage of

void * memcpy_s ( void * destination,
size_t dest_size,
const void * source,

                        size_t num );

vs. the original:

void * memcpy ( void * destination,
const void * source,

                      size_t num );

Time will tell, if memcpy_s() is actually "safer". If the programmer didn't think about the destination buffer's size before, why should he now? :->

Vinzent.

Neu: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate + Telefonanschluss für nur 17,95 Euro/mtl.!* http://dslspecial.gmx.de/freedsl-surfflat/?ac=OM.AD.PD003K11308T4569a

Reply by email

Marco van de Voort
to Vinzent Höfler

In our previous episode, "Vinzent H�fler" said:

Show quoted text 5 lines
> And of course, the move() procedure in FPC/TP/Delphi has exactly the > same problems as memcpy() as far as bounds-unsafety is concerned (it's > similar with fillchar/memset, indexbyte/memchr, etc). True. But in contrast to C an average programmer doesn't need it very often. In Pascal there are easier ways to assign arrays to each other than doing a raw byte copy. ;)

Less, yes.

Show quoted text 14 lines
But I still fail to see the advantage of void * memcpy_s ( void * destination, size_t dest_size, const void * source, size_t num ); vs. the original: void * memcpy ( void * destination, const void * source, size_t num ); Time will tell, if memcpy_s() is actually "safer". If the programmer didn't think about the destination buffer's size before, why should he now? :->

That's the feeling I had too. It will only matter in a very small number of items, where the numbers are actually calculated on the spot in different ways.

Reply by email

Vinzent Höfler
to Vinzent Höfler: Jonas Maebe : True. But in contrast to C an average programmer doesn't need it very…

"Vinzent Höfler" :

Time will tell, if memcpy_s() is actually "safer". If the programmer didn't think about the destination buffer's size before, why should he now? :->

The C(++)-folks fix:

-- 8< --
inline void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count) {
if (count > sizeInBytes)
throw InvalidArgument("memcpy_s: buffer overflow"); memcpy(dest, src, count);
}
-- 8< --

LMAO. Adding unnecessary bounds checking on each single call. So much for the speed advantage of a low-level language. A pascal compiler knows the correct sizes beforehand, no need for extra bounds checking...

Vinzent.

Neu: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate + Telefonanschluss für nur 17,95 Euro/mtl.!* http://dslspecial.gmx.de/freedsl-surfflat/?ac=OM.AD.PD003K11308T4569a

Reply by email

Jonas Maebe
to Vinzent Höfler
Vinzent Höfler wrote 6 lines
"Vinzent Höfler" : > Time will tell, if memcpy_s() is actually "safer". If the programmer > didn't think about the destination buffer's size before, why should > he > now? :->

Because the API forces him to think about it now.

Show quoted text 13 lines
The C(++)-folks fix: -- 8< -- inline void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count) { if (count > sizeInBytes) throw InvalidArgument("memcpy_s: buffer overflow"); memcpy(dest, src, count); } -- 8< -- LMAO. Adding unnecessary bounds checking on each single call.

I don't think it's unnecessary. Moreover, in the trivial cases (where you wouldn't use a move() in Pascal) the check will be optimized away (since all size-related arguments will be constants), and in most other cases the time spent on actually copying will probably dwarf any overhead added by that extra check.

So much for the speed advantage of a low-level language. A pascal compiler knows the correct sizes beforehand, no need for extra bounds checking...

A Pascal compiler has no idea about any actual variable sizes inside the move() procedure.

Jonas

Reply by email

Vinzent Höfler
to Jonas Maebe

Jonas Maebe :

Show quoted text 10 lines
On 16 May 2009, at 12:44, Vinzent Höfler wrote: > "Vinzent Höfler" : > >> Time will tell, if memcpy_s() is actually "safer". If the programmer >> didn't think about the destination buffer's size before, why should >> he >> now? :-> Because the API forces him to think about it now.

I wouldn't be to sure about that. See below.

Show quoted text 15 lines
> The C(++)-folks fix: > > -- 8< -- > inline void memcpy_s(void *dest, size_t sizeInBytes, const void > *src, size_t count) > { > if (count > sizeInBytes) > throw InvalidArgument("memcpy_s: buffer overflow"); > memcpy(dest, src, count); > } > -- 8< -- > > LMAO. Adding unnecessary bounds checking on each single call. I don't think it's unnecessary.

As necessary as in "the check will be optimized away"? ;)

Moreover, in the trivial cases (where you wouldn't use a move() in Pascal) the check will be optimized away (since all size-related arguments will be constants),

Hmmhmm. That means any static checker (like lint) should have caught the buffer overflow before the code even got compiled?

Well, C-code tends to forget any type information at the second call-level latest. Whilst a "sizeof" on a "char buffer[5]" declaration still works, "sizeof" yields different results for "char*". As "memcpy" itself demonstrates, both types are broadly equivalent, with the small difference that the latter is void of /any/ size information.

This simply doesn't happen with Pascal's arrays, even in non-trivial cases.

and in most other cases the time spent on actually copying will probably dwarf any overhead added by that extra check.

Sure. Tell that to the C-folks who keep complaining about slow languages like Pascal and Ada because of the runtime checks that happen there.

Show quoted text 6 lines
> So much for the speed advantage of a low-level language. A pascal > compiler knows the correct sizes beforehand, no need for extra > bounds checking... A Pascal compiler has no idea about any actual variable sizes inside the move() procedure.

But it usually knows it before entering it. C compilers don't.

Vinzent.

Neu: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate + Telefonanschluss für nur 17,95 Euro/mtl.!* http://dslspecial.gmx.de/freedsl-surfflat/?ac=OM.AD.PD003K11308T4569a

Reply by email