On Sat, Oct 9, 2010 at 9:36 AM, merlyn <[email protected]> wrote:
> Hi all,
>
> I'm facing really weird behavior of simple C program
> running under OpenBSD (4.7 and CURRENT).
>
> The code follows. The output also. The file to test the program with
> is also inline attached.
>
> The decomp procedure reads input, writes it to the stdout. All 4-chars
> preceeded by number are printer number times (e.g. 4abcd ->
> abcdabcdabcdabcd).
>
> ./a.c < test_file.txt
> should have output
> aaaaa bb
> ccccccccccc dddd
>
> however it sometimes ( 1 from 3 tryies, sometimes 1 from 20 or even 1
> from 40)
>
> looks like
> aaaaa bb
>
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
> cccccccccccccccccccccccccccccccccccccc
> dddd
>
>
> I've tryied this code with OpenBSD 4.7, OpenBSD CURRENT, CentOS Linux,
> FreeBSD 8.0 (all i386 arch), but the problem occurs only at OpenBSD (both
> versions).
>
> Does anyone know please where could be the problem? Because I have
> really no idea.
>
> Thanks a lot,
> Milan Bartos
>
>
>
> ---- CODE ---
>
> #include <stdio.h>
> #include <stdlib.h>
>
> void decomp(unsigned int N){
> char *pole;
> pole=malloc( sizeof(char) * N );
> char character;
> int i,j;
> int counter;
> while( (character = getchar()) != EOF){
> if( isdigit(character) ){
> counter=atoi(&character);
really? &character is _not_ a C-string. This is your bug.
--patrick
> for( i=0 ; i < N ; i++){
> pole[i]=getchar();
> }
> for( i=0 ; i < counter ; i++){
> for( j=0 ; j < N ; j++){
> putchar(pole[j]);
> }
> }
>
> }
> else {
> putchar(character);
> }
> }
> }
>
> int main(){
> unsigned int N=4;
> decomp(N);
> }
>
> ----- END OF CODE ---
>
>
>
> ----- OUTPUT ---
>
>> ./a < out4.txt
> aaaaa bb
> ccccccccccc dddd
>> ./a < out4.txt
> aaaaa bb
> ccccccccccc dddd
>> ./a < out4.txt
> aaaaa bb
>
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
> cccccccccccccccccccccccccccccccccccccc
> dddd
>> ./a < out4.txt
> aaaaa bb
> ccccccccccc dddd
>>
>
> ------ END OF OUTPUT ---
>
>
> ------ TEST FILE ---
> aaaaa bb
> 2ccccccc dddd
>
> ------ END OF TEST FILE ---
>
>
> --
> merlyn <[email protected]>
> OpenBSD mail.merlyn.cz 4.7 GENERIC.MP#1 i386
>
> [demime 1.01d removed an attachment of type application/pgp-signature]