1

So heres what the program is supposed to do: Restrict applications from opening on a mac by typing restrict . It is supposed to allow access to the application by typing restrict again using the same name as before.

What the program is doing instead: Restricting the application is working fine. But when I type restrict again, it comes up with the following output:

CandyBar \277_\377CandyBar \277_\377Restricting application chmod: /Applications/CandyBar \277_\377.app: No such file or directory chown: /Applications/CandyBar \277_\377.app: No such file or directory chmod: /Applications/CandyBar \277_\377.app: No such file or directory 

As you can see it adds the characters \277_\377 at the end of the string. Here is my source code:

for (int i = 0; strlen(argument) + 14 >= i; i++) { argument[i] = '\0'; } cout << argument; getArguments(); argument[strlen(argument) - 1] = '\0'; cout << argument; string application(argument); cout << application; if (!restrictedApplication[application]) { restrictedApplication[application] = false; } if (restrictedApplication[application] == false) { cout << "Restricting application\n"; restrictedApplication[application] = true; string fullCommand = "chmod -x '/Applications/" + application + ".app';" + "chown root '/Applications/" + application + ".app';" + "chmod 000 '/Applications/" + application + ".app'"; char fullCommandChar[256]; for (int i = 0; fullCommand[i] != '\0'; i++) { fullCommandChar[i] = fullCommand[i]; } system(fullCommandChar); } else { cout << "Restoring application\n"; restrictedApplication[application] = false; string fullCommand = "chmod +x '/Applications/" + application + ".app';" + "chown jamespickering '/Applications/" + application + ".app';" + "chmod 777 '/Applications/" + application + ".app'"; char fullCommandChar[256]; for (int i = 0; fullCommand[i] != '\0'; i++) { fullCommandChar[i] = fullCommand[i]; } system(fullCommandChar); } 
1
  • 1
    What does getArguments() do? How is argument declared? Commented Aug 30, 2013 at 19:52

2 Answers 2

5

It's probably because it's looking for the \0 character at the end of the string but it's never receiving it.

I haven't looked deep in your code yet but i see that you are trying to do that here.

argument[strlen(argument) - 1] = '\0'; 

I'll edit my post when I find where the \0 is going haywire because that's what produces that weird

\277_\377 output.


edit:

for (int i = 0; strlen(argument) + 14 >= i; i++) { argument[i] = '\0'; } 

What are you trying to do with this part of code?

This is essentially, looping through and adding \0 from index 0 to the length of the string plus 14 to argument. This should going passed argument's array size, correct? Can someone explain how this can even show an output?

Sign up to request clarification or add additional context in comments.

8 Comments

Array bounds are not checked at compile time, there's no reason for it not to compile.
@PaulGriffiths excuse me i meant to say produce an output.
It's hard to tell without seeing the definition of argument, but this wouldn't necessarily make it go out of bounds. strlen() returns the length of the string, not the length of the array. For instance, if he declared char argument[100] = "not much"; then it would be fine.
@PaulGriffiths Agreed. haha for instance he declared char fullCommandChar[256]; Thanks for clearing that up either way that loop seems to be the culprit.
Okay the +14 was added to try to get rid of the extraneous 14 characters mentioned at the top. @PaulGriffiths command getArguments() gets the argument after restrict and stores it in the char array argument
|
1

Here:

for (int i = 0; strlen(argument) + 14 >= i; i++) { argument[i] = '\0'; } cout << argument; 

you set the first element of argument to 0, and then try to output it. What exactly are you trying to output? You empty the string when you do that.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.