問題が発生しています_popen(const char *, const char *)
awk を使用して adb から情報を取得しようとしていますが、2 回目の _popen 呼び出しでプログラムがクラッシュします (メソッドが 2 回呼び出されます)。
問題のあるコードは次のとおりです(sysinf_getvalue()
メインで複数回呼び出されます)。
醜いコード
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;
}
注 :exe()
提供された同じ文字列 _popen を使用してメインで直接呼び出すと、クラッシュしません。
注2:デバッガーは言う"warning: HEAP[program.exe]:
warning: Heap block at 002D2EC0 modified at 002D2EC9 past requested size of 1"