0

The source code of square.c is:

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

int square(int *ptr)
{
  int a;
  a = *ptr;
  return a * a;
}

int main(int argc, char **argv)
{
  int a, aa;
  srandom(time(NULL));
  a = random() % 10 + 1;
  aa = square(&a);
  printf("%d\n", aa);
  return 0;
}

The command-line to compile the source code is:

gcc square.c -o square

Is it possible to run the square executable in Linux so that the printed value will not be a square of any integer number?

Any method of running the program is allowed.

  • homework? job interview? please tag it appropriately. – amit Jan 16 '12 at 14:24
  • Why are you trying to write a correct program and then try to get an unexpected output from it? – Alok Save Jan 16 '12 at 14:24
  • Why do you want to do this? I guess you could run it through a debugger, brake and change the value of aa before it prints. – steabert Jan 16 '12 at 14:25
  • 1
    Seems to me that this is off-topic, being about how to run a program rather than how to do programming. Even if you stretch things and fit it in as part of a shell script, that just seems to shift it from "off topic" to "not productive". – Jerry Coffin Jan 16 '12 at 14:26
  • change libc. hexedit the binary. – ChuckCottrill Oct 21 '13 at 19:39

4 Answers4

6

Yes. We can override printf.

Write the code in your post into square.c and compile it with gcc square.c

Make this file, fakesquare.c

int printf(char *str,int i)
{
    return puts("7");
}

Compile fakesquare.c as a shared library:

gcc -fPIC -o libfakesquare.so -shared fakesquare.c

Run the square program with libfakesquare.so preloaded:

[15:27:27 0 /tmp] $ LD_PRELOAD=./libfakesqare.so ./a.out
7
[15:29:16 0 /tmp] $ LD_PRELOAD=./libfakesqare.so ./a.out
7
[15:29:16 0 /tmp] $ LD_PRELOAD=./libfakesqare.so ./a.out
7

Witout libfakeshared.so preloaded:

[15:29:40 0 /tmp] $  ./a.out
36
[15:29:41 0 /tmp] $  ./a.out
16
[15:29:42 0 /tmp] $  ./a.out
64
nos
  • 223,662
  • 58
  • 417
  • 506
0

The only dependency at your code is libc. If libc stays unmodified then your code will always work.

Also your program will fail if before running it, all available memory is exhausted. You can always check if ptr!=NULL.

cateof
  • 6,608
  • 25
  • 79
  • 153
  • 1
    I don't see how random() returning 0 should make square() fail. – Werner Henze Jan 16 '12 at 14:43
  • the square of 0 is 0. However running ./a.out will always print 0, thereafter we don't have a program that prints a _random_ square. The output will be _constant_ – cateof Jan 16 '12 at 14:55
  • 2
    The code is `a = random() % 10 + 1`, so a would be 1 and the output would be 1. Besides that, the original question was if the program might in any run print a value that is not the square of an integer number. 0 and 1 are the square of an integer number. (Of course you are right that the program might not do what it is intended to do.) – Werner Henze Jan 16 '12 at 15:00
  • 1
    sure no problem. I wanted to emphasize that modifying libc.so somehow it might result to wrong square. – cateof Jan 16 '12 at 15:25
0

You could use this :

Fastest way to determine if an integer's square root is an integer

Their code seems optimized, but whichever is simplest should do the trick for you.

Community
  • 1
  • 1
Kevin Coulombe
  • 1,517
  • 1
  • 17
  • 35
0

Assuming a standard C environment I don't see a reason why this should fail on a standard platform. The code might fail if printf is not doing what it is inteded to do, but probably this is not what you are asking for. It also might fail on a platform where int is as small as a byte and a byte is only 6 bits wide. In this case your square function might calculate 9*9=81 which will not fit in the result type int (0..63 for 6 bit-byte). But in my opinion this is a quite academic case.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69