Eric Hoaglin wrote:

I have the following code:
http://www.pasteit4me.com/763005

I'm trying to capture the output of the task that I run. and from what I understand, if you want to do so, you pass a FILE* to AuthorizationExecuteWithPrivileges and then just read it as a normal file (which you can see from lines 29-45


Your code has a serious bug.

        if(taskFile != NULL) {
        const int BYTES_TO_READ = 64;
        char *theCString = malloc(sizeof(char) * BYTES_TO_READ);
        int bufferSize = 64;
        int bytesRead = 0;
        int totalBytes = 0;
while((bytesRead = fread(theCString, 1, BYTES_TO_READ, taskFile)) != 0) {
                bufferSize += 64;
                theCString = realloc(theCString, bufferSize);
                totalBytes += bytesRead;
        }
        if (taskFile != NULL) { 
                NSLog(@"Total Bytes Read: %i", totalBytes);
*tResults = [[NSString alloc] initWithBytesNoCopy:theCString length:totalBytes encoding:NSUTF8StringEncoding freeWhenDone:YES];
        }

Your fread() is always reading data into the start of the buffer 'theCString'. This is wrong. There should be a separate pointer that's advanced by the number of bytes just read. Or use fread (theCString+totalBytes,...).

You also have a stylistic flaw of repeated use of the magic number 64, despite defining a constant BYTES_TO_READ to represent it.

Another possible problem is you don't test the result returned from AEWP(). You go straight into reading the FILE, and only test the result later as part of a questionable "retry" block, not shown above. There is a dubious busy loop in the "retry", and there may be other problems.

Is the fread() bug the cause of the problem you posted about? I don't know. But it certainly is a significant problem, and it has a huge effect on what data gets returned as the "output of the task" string.

You never said what you were actually executing with elevated privileges. If I were you, I'd test your code by running it on /bin/ echo or /usr/bin/id. Both of those executables are well characterized and well behaved, and they emit text only on stdout. If those fail in the same way, then make a well-isolated fully compilable example and post its source.

The first step to solving the problem is to replicate it, but you haven't provided enough code to do that.

  -- GG

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to