Исправлены проблемы с выделением памяти

This commit is contained in:
Alexander Zhirov 2022-07-19 13:13:57 +03:00
parent 6eac429923
commit d74ba6de54
8 changed files with 91 additions and 50 deletions

44
CMakeLists.txt Normal file
View File

@ -0,0 +1,44 @@
cmake_minimum_required(VERSION 2.8)
project(xfree)
#include_directories(/home/alexander/repositories/iup/include)
include_directories(/root/iup/include)
set(SOURCE_EXE
address.c
arguments.c
concat.c
db.c
gui.c
images.c
monitor.c
node_settings.c
rxrandr_broker.c
rxrandr.c
settings.c
xfreerdp.c
xrdp.c
)
add_executable(xfree ${SOURCE_EXE})
#target_link_libraries(xfree iup X11 Xrandr m /home/alexander/programming/c/iup/sqlite3.a)
target_link_libraries(xfree
gtk-3
gdk-3
pangocairo-1.0
pango-1.0
atk-1.0
cairo-gobject
cairo
gdk_pixbuf-2.0
gio-2.0
gobject-2.0
glib-2.0
X11
Xrandr
m
dl
pthread
/root/iup/lib/Linux515/libiup.a
/root/SQLite/sqlite3.a
)
#target_link_libraries(xfree iup X11 Xrandr m sqlite3 -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lX11 -lXrandr -lm)

View File

@ -21,8 +21,9 @@ char *getHostIP(char *dnsName)
if (he) if (he)
{ {
char *ip = inet_ntoa(*(struct in_addr*)he->h_addr); char *ip = inet_ntoa(*(struct in_addr*)he->h_addr);
result = (char *)malloc(sizeof(char) * strlen(ip)); size_t size = strlen(ip) + 1;
strcpy(result, ip); result = (char *)malloc(sizeof(char) * size);
strncpy(result, ip, size);
} }
return result; return result;
} }

View File

@ -26,11 +26,11 @@ char* concat(char *s1, char *s2)
if (s1) if (s1)
{ {
strcpy(result, s1); strncpy(result, s1, (len1 + 1));
} }
if (s2) if (s2)
{ {
strcpy(result + len1, s2); strncpy(result + len1, s2, (len2 + 1));
} }
return result; return result;

33
db.c
View File

@ -6,6 +6,7 @@
*/ */
#include "sqlite3.h" #include "sqlite3.h"
//#include <sqlite3.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -15,7 +16,7 @@
char* getPathDB(char *path) char* getPathDB(char *path)
{ {
static char *current = NULL; static char *current = NULL;
if (path) if (path && !current)
{ {
current = path; current = path;
} }
@ -53,8 +54,9 @@ static int dbLoad(void *NotUsed, int argc, char **argv, char **azColName)
{ {
if (!getParameter(atoi(argv[0]))) if (!getParameter(atoi(argv[0])))
{ {
addParameterKey(atoi(argv[0]), argv[1], atoi(argv[2]), atoi(argv[3]), argv[7] ? getParameter(atoi(argv[7])) : NULL, addParameterKey(atoi(argv[0]), argv[1], atoi(argv[2]), atoi(argv[3]),
argv[8] ? getParameter(atoi(argv[8])) : NULL); (argv[7] ? getParameter(atoi(argv[7])) : NULL),
(argv[8] ? getParameter(atoi(argv[8])) : NULL));
} }
if (argv[4] && argv[5]) if (argv[4] && argv[5])
@ -97,7 +99,6 @@ bool dbLoadData()
static int dbCreateHostsList(void *answer, int argc, char **argv, char **azColName) static int dbCreateHostsList(void *answer, int argc, char **argv, char **azColName)
{ {
fprintf(stderr, "dbCreateHostsList 1\n");
Hosts *hosts = *(Hosts**) answer; Hosts *hosts = *(Hosts**) answer;
Host *host = (Host*) malloc(sizeof(Host)); Host *host = (Host*) malloc(sizeof(Host));
host->data = (char**) malloc(sizeof(char*) * argc); host->data = (char**) malloc(sizeof(char*) * argc);
@ -105,12 +106,11 @@ static int dbCreateHostsList(void *answer, int argc, char **argv, char **azColNa
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
{ {
host->data[i] = (char*) malloc(sizeof(char) * strlen(argv[i])); size_t size = strlen(argv[i]) + 1;
strcpy(host->data[i], argv[i]); host->data[i] = (char*) malloc(sizeof(char) * size);
strncpy(host->data[i], argv[i], size);
} }
fprintf(stderr, "dbCreateHostsList 2\n");
Host **tmp = hosts->host; Host **tmp = hosts->host;
hosts->host = (Host**) malloc(sizeof(Host*) * ++hosts->size); hosts->host = (Host**) malloc(sizeof(Host*) * ++hosts->size);
for (size_t i = 0; i < hosts->size - 1; ++i) for (size_t i = 0; i < hosts->size - 1; ++i)
@ -123,14 +123,11 @@ static int dbCreateHostsList(void *answer, int argc, char **argv, char **azColNa
} }
hosts->host[hosts->size - 1] = host; hosts->host[hosts->size - 1] = host;
fprintf(stderr, "dbCreateHostsList 3\n");
return 0; return 0;
} }
Hosts* dbGetHostsList() Hosts* dbGetHostsList()
{ {
fprintf(stderr, "dbGetHostsList 1\n");
sqlite3 *db = dbGetBase(getPathDB(NULL)); sqlite3 *db = dbGetBase(getPathDB(NULL));
if (!db) if (!db)
{ {
@ -139,7 +136,6 @@ Hosts* dbGetHostsList()
char *err_msg = 0; char *err_msg = 0;
char *sql = "SELECT ROW_NUMBER () OVER (ORDER BY `hosts`.`ip`) `item`, `hosts`.`dns` as `dns`, `hosts`.`set` as `set` FROM `hosts` as `hosts`"; char *sql = "SELECT ROW_NUMBER () OVER (ORDER BY `hosts`.`ip`) `item`, `hosts`.`dns` as `dns`, `hosts`.`set` as `set` FROM `hosts` as `hosts`";
fprintf(stderr, "dbGetHostsList 2\n");
Hosts *hosts = (Hosts*) malloc(sizeof(Hosts)); Hosts *hosts = (Hosts*) malloc(sizeof(Hosts));
hosts->size = 0; hosts->size = 0;
hosts->host = NULL; hosts->host = NULL;
@ -151,9 +147,7 @@ Hosts* dbGetHostsList()
sqlite3_close(db); sqlite3_close(db);
exit(-1); exit(-1);
} }
fprintf(stderr, "dbGetHostsList 3\n");
sqlite3_close(db); sqlite3_close(db);
fprintf(stderr, "dbGetHostsList 4\n");
return hosts; return hosts;
} }
@ -399,8 +393,9 @@ static int dbCreateMonitorsList(void *answer, int argc, char **argv, char **azCo
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
{ {
monitor->data[i] = (char*) malloc(sizeof(char) * strlen(argv[i])); size_t size = strlen(argv[i]) + 1;
strcpy(monitor->data[i], argv[i]); monitor->data[i] = (char*) malloc(sizeof(char) * size);
strncpy(monitor->data[i], argv[i], size);
} }
Monitor **tmp = monitors->monitor; Monitor **tmp = monitors->monitor;
@ -423,7 +418,7 @@ Monitors* dbGetMonitorsList()
sqlite3 *db = dbGetBase(getPathDB(NULL)); sqlite3 *db = dbGetBase(getPathDB(NULL));
if (!db) if (!db)
{ {
return false; exit(-1);
} }
char *err_msg = 0; char *err_msg = 0;
@ -436,10 +431,10 @@ Monitors* dbGetMonitorsList()
if (sqlite3_exec(db, sql, dbCreateMonitorsList, &monitors, &err_msg) != SQLITE_OK) if (sqlite3_exec(db, sql, dbCreateMonitorsList, &monitors, &err_msg) != SQLITE_OK)
{ {
fprintf(stderr, "Ошибка выполнения запроса: %s\n", err_msg); fprintf(stderr, "Получение списка мониторов. Ошибка выполнения запроса: %s\n", err_msg);
sqlite3_free(err_msg); sqlite3_free(err_msg);
sqlite3_close(db); sqlite3_close(db);
exit(-1); exit(-2);
} }
sqlite3_close(db); sqlite3_close(db);

33
gui.c
View File

@ -58,25 +58,24 @@ static int guiConnect(Ihandle *self)
static void createHostsList(Ihandle *iupList) static void createHostsList(Ihandle *iupList)
{ {
fprintf(stderr, "ERROR 1\n");
Hosts *hosts = dbGetHostsList(); Hosts *hosts = dbGetHostsList();
fprintf(stderr, "ERROR 2\n");
char *indexItem = NULL; char *indexItem = NULL;
char *serverName = NULL; char *serverName = NULL;
for (size_t i = 0; i < hosts->size; ++i) for (size_t i = 0; i < hosts->size; ++i)
{ {
Host *host = hosts->host[i]; Host *host = hosts->host[i];
indexItem = (char *)malloc(sizeof(char) * strlen(host->data[0])); size_t size = strlen(host->data[0]) + 1;
strcpy(indexItem, host->data[0]); indexItem = (char *)malloc(sizeof(char) * size);
serverName = (char *)malloc(sizeof(char) * strlen(host->data[1])); strncpy(indexItem, host->data[0], size);
strcpy(serverName, host->data[1]); size = strlen(host->data[1]) + 1;
serverName = (char *)malloc(sizeof(char) * size);
strncpy(serverName, host->data[1], size);
IupSetAttribute(iupList, indexItem, serverName); IupSetAttribute(iupList, indexItem, serverName);
if (atoi(host->data[2])) if (atoi(host->data[2]))
IupSetAttribute(iupList, "VALUE", serverName); IupSetAttribute(iupList, "VALUE", serverName);
} }
fprintf(stderr, "ERROR 3\n");
dbFreeHosts(hosts); dbFreeHosts(hosts);
} }
@ -107,15 +106,13 @@ Ihandle* guiStart()
inputServer = IupList(NULL); inputServer = IupList(NULL);
IupSetAttribute(inputServer, "NAME", "SERVER"); IupSetAttribute(inputServer, "NAME", "SERVER");
IupSetAttribute(inputServer, "TIP", "IP-адрес или имя удаленного сервера (обязательно)"); IupSetAttribute(inputServer, "TIP", "IP-адрес или имя удаленного сервера (обязательно)");
fprintf(stderr, "guiStart 1\n");
createHostsList(inputServer); createHostsList(inputServer);
fprintf(stderr, "guiStart 2\n");
IupSetAttribute(inputServer, "SIZE", "100"); IupSetAttribute(inputServer, "SIZE", "100");
IupSetAttribute(inputServer, "EDITBOX", "YES"); IupSetAttribute(inputServer, "EDITBOX", "YES");
IupSetAttribute(inputServer, "DROPDOWN", "YES"); IupSetAttribute(inputServer, "DROPDOWN", "YES");
hBoxServer = IupHbox(labelServer, inputServer, NULL); hBoxServer = IupHbox(labelServer, inputServer, NULL);
IupSetAttribute(hBoxServer, "MARGIN", "10x10"); IupSetAttribute(hBoxServer, "MARGIN", "10x10");
fprintf(stderr, "guiStart 3\n");
labelUser = IupLabel("Пользователь:"); labelUser = IupLabel("Пользователь:");
IupSetAttribute(labelUser, "SIZE", "80x16"); IupSetAttribute(labelUser, "SIZE", "80x16");
IupSetAttribute(labelUser, "ALIGNMENT", "ARIGHT:ACENTER"); IupSetAttribute(labelUser, "ALIGNMENT", "ARIGHT:ACENTER");
@ -127,7 +124,7 @@ Ihandle* guiStart()
IupSetAttribute(inputUser, "SIZE", "100"); IupSetAttribute(inputUser, "SIZE", "100");
hBoxUser = IupHbox(labelUser, inputUser, NULL); hBoxUser = IupHbox(labelUser, inputUser, NULL);
IupSetAttribute(hBoxUser, "MARGIN", "10x0"); IupSetAttribute(hBoxUser, "MARGIN", "10x0");
fprintf(stderr, "guiStart 4\n");
labelPassword = IupLabel("Пароль:"); labelPassword = IupLabel("Пароль:");
IupSetAttribute(labelPassword, "SIZE", "80x16"); IupSetAttribute(labelPassword, "SIZE", "80x16");
IupSetAttribute(labelPassword, "ALIGNMENT", "ARIGHT:ACENTER"); IupSetAttribute(labelPassword, "ALIGNMENT", "ARIGHT:ACENTER");
@ -140,38 +137,38 @@ Ihandle* guiStart()
IupSetAttribute(inputPassword, "PASSWORD", "YES"); IupSetAttribute(inputPassword, "PASSWORD", "YES");
hBoxPassword = IupHbox(labelPassword, inputPassword, NULL); hBoxPassword = IupHbox(labelPassword, inputPassword, NULL);
IupSetAttribute(hBoxPassword, "MARGIN", "10x10"); IupSetAttribute(hBoxPassword, "MARGIN", "10x10");
fprintf(stderr, "guiStart 5\n");
btnConnect = IupButton("Подключение", NULL); btnConnect = IupButton("Подключение", NULL);
IupSetHandle("CONNECT", btnConnect); IupSetHandle("CONNECT", btnConnect);
IupSetAttribute(btnConnect, "NAME", "CONNECT"); IupSetAttribute(btnConnect, "NAME", "CONNECT");
IupSetAttribute(btnConnect, "TIP", "Выполнить подключение"); IupSetAttribute(btnConnect, "TIP", "Выполнить подключение");
IupSetAttribute(btnConnect, "BGCOLOR", "0 179 0"); IupSetAttribute(btnConnect, "BGCOLOR", "0 179 0");
IupSetAttribute(btnConnect, "FGCOLOR", "255 255 255"); IupSetAttribute(btnConnect, "FGCOLOR", "255 255 255");
fprintf(stderr, "guiStart 6\n");
btnClose = IupButton("Закрыть", NULL); btnClose = IupButton("Закрыть", NULL);
IupSetHandle("CLOSE", btnClose); IupSetHandle("CLOSE", btnClose);
IupSetAttribute(btnClose, "NAME", "CLOSE"); IupSetAttribute(btnClose, "NAME", "CLOSE");
IupSetAttribute(btnClose, "TIP", "Закрыть FreeRDP"); IupSetAttribute(btnClose, "TIP", "Закрыть FreeRDP");
IupSetAttribute(btnClose, "BGCOLOR", "204 0 0"); IupSetAttribute(btnClose, "BGCOLOR", "204 0 0");
IupSetAttribute(btnClose, "FGCOLOR", "255 255 255"); IupSetAttribute(btnClose, "FGCOLOR", "255 255 255");
fprintf(stderr, "guiStart 7\n");
btnSettings = IupButton("Настройки", NULL); btnSettings = IupButton("Настройки", NULL);
IupSetAttribute(btnSettings, "NAME", "SETTINGS"); IupSetAttribute(btnSettings, "NAME", "SETTINGS");
IupSetAttribute(btnSettings, "TIP", "Перейти в настройки"); IupSetAttribute(btnSettings, "TIP", "Перейти в настройки");
IupSetAttribute(btnSettings, "BGCOLOR", "0 136 204"); IupSetAttribute(btnSettings, "BGCOLOR", "0 136 204");
IupSetAttribute(btnSettings, "FGCOLOR", "255 255 255"); IupSetAttribute(btnSettings, "FGCOLOR", "255 255 255");
fprintf(stderr, "guiStart 8\n");
hBoxButtons = IupHbox(IupFill(), btnSettings, btnConnect, btnClose, IupFill(), NULL); hBoxButtons = IupHbox(IupFill(), btnSettings, btnConnect, btnClose, IupFill(), NULL);
IupSetAttribute(hBoxButtons, "ALIGNMENT", "ACENTER:ACENTER"); IupSetAttribute(hBoxButtons, "ALIGNMENT", "ACENTER:ACENTER");
IupSetAttribute(hBoxButtons, "GAP", "10"); IupSetAttribute(hBoxButtons, "GAP", "10");
IupSetAttribute(hBoxButtons, "MARGIN", "10x10"); IupSetAttribute(hBoxButtons, "MARGIN", "10x10");
fprintf(stderr, "guiStart 9\n");
vBoxMain = IupVbox(labelImage, hBoxServer, hBoxUser, hBoxPassword, hBoxButtons, NULL); vBoxMain = IupVbox(labelImage, hBoxServer, hBoxUser, hBoxPassword, hBoxButtons, NULL);
IupSetCallback(btnConnect, "ACTION", (Icallback) guiConnect); IupSetCallback(btnConnect, "ACTION", (Icallback) guiConnect);
IupSetCallback(btnClose, "ACTION", (Icallback) guiExit); IupSetCallback(btnClose, "ACTION", (Icallback) guiExit);
IupSetCallback(btnSettings, "ACTION", (Icallback) settingsMainWindow); IupSetCallback(btnSettings, "ACTION", (Icallback) settingsMainWindow);
fprintf(stderr, "guiStart 10\n");
dlg = IupDialog(vBoxMain); dlg = IupDialog(vBoxMain);
IupSetAttribute(dlg, "IMAGE", "BG_HEAD"); IupSetAttribute(dlg, "IMAGE", "BG_HEAD");
IupSetAttribute(dlg, "ICON", "icon"); IupSetAttribute(dlg, "ICON", "icon");
@ -180,6 +177,6 @@ Ihandle* guiStart()
IupSetAttribute(dlg, "PARENTDIALOG", "MAIN_WINDOW"); IupSetAttribute(dlg, "PARENTDIALOG", "MAIN_WINDOW");
IupSetAttribute(dlg, "DEFAULTENTER", "CONNECT"); IupSetAttribute(dlg, "DEFAULTENTER", "CONNECT");
IupSetAttribute(dlg, "DEFAULTESC", "CLOSE"); IupSetAttribute(dlg, "DEFAULTESC", "CLOSE");
fprintf(stderr, "guiStart 11\n");
return dlg; return dlg;
} }

View File

@ -24,8 +24,9 @@ static NodeValue* newNodeValue(Value name, char *current, bool set)
node->change = false; node->change = false;
node->next = NULL; node->next = NULL;
node->current = (char*) malloc(sizeof(char) * strlen(current)); size_t size = strlen(current) + 1;
strcpy(node->current, current); node->current = (char*) malloc(sizeof(char) * size);
strncpy(node->current, current, size);
return node; return node;
} }
@ -63,8 +64,9 @@ static NodeParameter* newNodeParameter(Parameter name, char *key, bool set, bool
node->conflict = conflict; node->conflict = conflict;
node->next = NULL; node->next = NULL;
node->key = (char*) malloc(sizeof(char) * strlen(key)); size_t size = strlen(key) + 1;
strcpy(node->key, key); node->key = (char*) malloc(sizeof(char) * size);
strncpy(node->key, key, size);
return node; return node;
} }
@ -304,7 +306,7 @@ void setParameterValue(Parameter pName, Value vName, char *current)
return; return;
} }
size_t length = strlen(current); size_t length = strlen(current) + 1;
if (!length) if (!length)
{ {
@ -338,7 +340,7 @@ void setParameterValue(Parameter pName, Value vName, char *current)
{ {
free(nodeValue->current); free(nodeValue->current);
nodeValue->current = (char*) malloc(sizeof(char) * length); nodeValue->current = (char*) malloc(sizeof(char) * length);
strcpy(nodeValue->current, current); strncpy(nodeValue->current, current, length);
} }
else else
{ {

View File

@ -22,8 +22,8 @@ typedef enum
PARAMETER_SOUND, PARAMETER_SOUND,
PARAMETER_COMPRESSION, PARAMETER_COMPRESSION,
PARAMETER_FULLSCREEN, PARAMETER_FULLSCREEN,
PARAMETER_MONITORS,
PARAMETER_MULTIMONITOR, PARAMETER_MULTIMONITOR,
PARAMETER_MONITORS,
PARAMETER_AUTHENTICATION, PARAMETER_AUTHENTICATION,
PARAMETER_SECURITY, PARAMETER_SECURITY,
PARAMETER_BITSPERPIXEL, PARAMETER_BITSPERPIXEL,

View File

@ -19,11 +19,13 @@ x_info *getXInfo()
for (int i = 0; i < monitors->count; ++i) for (int i = 0; i < monitors->count; ++i)
{ {
monitors->monitor[i].ptrName = (char *)malloc(sizeof(char) * strlen(monitors->monitor[i].name)); size_t size = strlen(monitors->monitor[i].name) + 1;
monitors->monitor[i].ptrName = (char *)malloc(sizeof(char) * size);
monitors->monitor[i].ptrIndexItem = (char *)malloc(sizeof(char) * 3); monitors->monitor[i].ptrIndexItem = (char *)malloc(sizeof(char) * 3);
monitors->monitor[i].ptrIndexMonitor = (char *)malloc(sizeof(char) * 3); monitors->monitor[i].ptrIndexMonitor = (char *)malloc(sizeof(char) * 3);
strcpy(monitors->monitor[i].ptrName, monitors->monitor[i].name); strncpy(monitors->monitor[i].ptrName, monitors->monitor[i].name, size);
sprintf(monitors->monitor[i].ptrIndexItem, "%d", i + 1); sprintf(monitors->monitor[i].ptrIndexItem, "%d", i + 1);
sprintf(monitors->monitor[i].ptrIndexMonitor, "%d", i); sprintf(monitors->monitor[i].ptrIndexMonitor, "%d", i);
} }