NO

Author Topic: memset bug  (Read 4580 times)

akko

  • Guest
memset bug
« on: July 30, 2012, 03:49:45 PM »
Run the following without and with compiler optimization  (PC rel 7)

Code: [Select]
// PC7 memmove/set test #2

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void me_move(char* ato, char* afrom, int u)
{ if (afrom < ato) // down
{ afrom += u, ato += u;
while (u--) { *--ato = *--afrom; } }
else // up
{ while (u--) { *ato++ = *afrom++; } }
}

int main(void)
{ char *str; int i;

str = malloc(4);
strcpy(str,"abc");
printf("lib moving %s :",str);

memmove(str+1,str,2);
printf("\naab ? %s",str);
if (strcmp(str,"aab")) printf("\t<< error");

memmove(str,str+1,2);
printf("\nabb ? %s",str);
if (strcmp(str,"abb")) printf("\t<< error");

strcpy(str,"abc");
printf("\n\nme moving %s :",str);

me_move(str+1,str,2);
printf("\naab ? %s",str);
if (strcmp(str,"aab")) printf("\t<< error");

me_move(str,str+1,2);
printf("\nabb ? %s\n",str);
if (strcmp(str,"abb")) printf("\t<< error");

memset(str,-1,4);
printf("\nlib filling :");
i = *(int*)str;
printf("\n-1 ? %d",i);
if (i != -1) printf("\t<< error");
puts("");

return 0;
}


PauloH

  • Guest
Re: memset bug
« Reply #1 on: August 15, 2012, 06:59:23 PM »
I found this bug too.

kst

  • Guest
Re: memset bug
« Reply #2 on: August 30, 2012, 10:45:48 PM »
I just tried this myself with Pelles C for Windows 7.00.350 (Win64).

There are 5 different optimization options: "None", "Maximize speed", "Minimize size", "Maximize speed more", "Minimize size more".

I get the same output with all 5 options.

Can you be more specific about (a) how to reproduce the problem, and (b) what the problem is?

akko

  • Guest
Re: memset bug
« Reply #3 on: August 31, 2012, 01:00:58 PM »
Well, I am using Win7 and PellesC Rel7 32-bit, not 64-bit.

It seems to make the difference.


Rgds Akko

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: memset bug
« Reply #4 on: September 01, 2012, 09:42:15 PM »
I will have to analyze this first, before I can saying anything else...
/Pelle

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: memset bug
« Reply #5 on: September 02, 2012, 02:27:29 PM »
Code: [Select]
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#pragma optimize(time)
void test1(char *str, int cnt) {
memset(str,-1,cnt);
}
#pragma optimize(size)
void test2(char *str, int cnt) {
memset(str,-1,cnt);
}
#pragma optimize(none)
void test3(char *str, int cnt) {
memset(str,-1,cnt);
}
#pragma optimize()
void printit(char *msg, char *str, int cnt){
int err = 0;
int i;
printf("%s: ", msg);
for (i = 0; i < cnt; i++) {
printf("%02X", (unsigned char)str[i]);
if ((unsigned char)str[i] != 0xFF) err++;
}
if (!err) printf(" OK\n");
else printf(" %d ERRORS\n", err);
}
int main(void)
{
char *str;
int i;
for (i = 1; i < 20; i++) {
str = malloc(i);
//memset(str,-1,4);
test1(str, i);
printit("time", str, i);
test2(str, i);
printit("size", str, i);
test3(str, i);
printit("none", str, i);
free(str);
printf("\n");
}
return 0;
}
May the source be with you