【问题标题】:Calling multiple _popen fails调用多个 _popen 失败
【发布时间】:2013-01-03 15:38:47
【问题描述】:

_popen(const char *, const char *) 有问题

我正在尝试使用 awk 从 adb 获取信息,但我的程序在第二次 _popen 调用时崩溃(该方法被调用了两次)。

这是有问题的代码(sysinf_getvalue() 在 main 中被多次调用)。


丑陋的代码

int deviceadbinfowritten = 0;
int devicebootloaderinfowritten = 0;
int adbinit = 0;

int initadb()
{
    if(!adbinit)
    {
        system("adb kill-server && adb start-server");
        adbinit = 1;
    }
    return 0;
}

int sysinfo_getadbinfofile()
{
    if(!deviceadbinfowritten)
    {
        system("echo Please connect your device && adb wait-for-device && adb.exe shell getprop > deviceinfo");
        deviceadbinfowritten = 1;
    }

    return 0;
}

int sysinfo_getbootloaderinfofile()
{
    if(!devicebootloaderinfowritten)
    {
        system("adb wait-for-device && adb reboot bootloader && fastboot getvar all > bootloaderinfo && fastboot reboot");
        devicebootloaderinfowritten = 1;
    }
    return 0;
}

char *sysinfo_getvalue(char *key, int bootloader)
{
    initadb();
    if(bootloader) sysinfo_getbootloaderinfofile();
    else sysinfo_getadbinfofile();
    char *file = (bootloader)?"bootloaderinfo":"deviceinfo";

    char *command = malloc(sizeof(char) * (strlen("awk.exe -F\":\" \" { print $2 }\" < ") + strlen(key) + (bootloader)?0:2 + strlen(file)));
    char *adbkey = malloc((strlen(key)+2)* (sizeof(char)));
    if(!bootloader) sprintf(adbkey, "/%s/", key);
    sprintf(command, "awk.exe -F\":\" \"%s { print $2 }\" < %s", ((bootloader)?key:adbkey), file);

    printf("%s\n", command);
    char *pointer = exe(command);
    if(!bootloader)
    {
        pointer += 2;
        pointer[strlen(pointer)-3] = 0;
    }

    return pointer;
}

char *exe(char *exe)
{
    char buffer[1024];
    FILE *f;

    printf("debug %s\n", exe);

    f = _popen(exe, "r");

    printf("debug\n");

    char *res = NULL;

    printf("debug\n");

    if (f == NULL) {
        printf("[-] Failed to run command\n" );
        return NULL;
}
    printf("debug\n");

    while (fgets(buffer, sizeof(buffer), f) != NULL) {
        if(res == NULL) {res = malloc(strlen(buffer) * sizeof(char)); strcpy(res, buffer);}
        else {res = realloc(res, strlen(res) * sizeof(char) + sizeof(buffer));sprintf(res, "%s%s", res, buffer);}
}

    printf("debug\n");

    _pclose(f);

    return res;
}

注意:如果我在 main 中使用提供的相同字符串直接调用 exe() _popen 不会崩溃。

注意 2:调试器说 "warning: HEAP[program.exe]: warning: Heap block at 002D2EC0 modified at 002D2EC9 past requested size of 1"

【问题讨论】:

  • 代码缺少对系统调用的错误检查(如system()malloc())。此外,它似乎没有释放通过malloc() 分配的任何内存。

标签: c windows popen


【解决方案1】:

您似乎缺少为 0 终止符分配空间。

这是每个字符数组需要的最后一个额外字节,如果用作字符串以用值NUL(十进制0)的字符指示字符串的结尾。

至少这一行:

char *command = malloc(sizeof(char) * (strlen("awk.exe -F\":\" \" { print $2 }\" < ") + strlen(key) + (bootloader)?0:2 + strlen(file)));

应多分配一个字符:

char *command = malloc(
  sizeof(char) * (
    strlen("awk.exe -F\":\" \" { print $2 }\" < ") + 
    strlen(key) + 
    (bootloader)?0:2 + strlen(file)) + 
    1
  )
);

(对于此类问题,我没有检查代码中字符数组的所有分配/声明。)

【讨论】:

  • 哦,谢谢,我忘了strlen() 包含 0terminator。
  • 代码仍然崩溃,试图找出原因但我不能。
  • @Freesalad:您可能想查看malloc() 参与的其他案例。同样使用calloc() 可能会有所帮助,因为它将内存初始化为全0。
【解决方案2】:

我在此链接C++ system() not working when there are spaces in two different parameters 中发现,如果您希望它们正常工作,您需要用引号将所有系统调用封装起来。 例如在我的代码中,我需要使用exe("\"mycommand\""); 而不是exe("mycommand");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多