make sure previously allocated mem get free()'d if subsequent allocactions fail

This commit is contained in:
marschap
2008-12-14 09:59:34 +00:00
parent 4b11c5aa17
commit 99928e7303
2 changed files with 69 additions and 14 deletions
+68 -14
View File
@@ -1,5 +1,5 @@
/** \file server/screen.c
* Does screen management
* Does screen management.
*/
/* This file is part of LCDd, the lcdproc server.
@@ -9,6 +9,7 @@
*
* Copyright (c) 1999, William Ferrell, Scott Scriven
* 2003, Joris Robijn
* 2008, Peter Marschall
*/
#include <stdlib.h>
@@ -40,30 +41,39 @@ char *pri_names[] = {
NULL,
};
/** Create a screen.
* \param id Screen id; it's name.
* \param client Client, the screen belongs to.
* \return Pointer to freshly created screen.
*/
Screen *
screen_create(char *id, Client *client)
{
Screen *s;
debug(RPT_DEBUG, "%s(id=\"%.40s\", client=[%d])", __FUNCTION__, id, (client?client->sock:-1));
debug(RPT_DEBUG, "%s(id=\"%.40s\", client=[%d])",
__FUNCTION__, id, (client?client->sock:-1));
s = malloc(sizeof(Screen));
if (!s) {
report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
return NULL;
}
if (!id) {
report(RPT_ERR, "%s: Need id string", __FUNCTION__);
return NULL;
}
/* Client can be NULL for serverscreens and other client-less screens */
s->id = strdup(id);
if (!s->id) {
s = malloc(sizeof(Screen));
if (s == NULL) {
report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
return NULL;
}
s->id = strdup(id);
if (s->id == NULL) {
report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
free(s);
return NULL;
}
s->name = NULL;
s->priority = PRI_INFO;
s->duration = default_duration;
@@ -82,8 +92,10 @@ screen_create(char *id, Client *client)
s->cursor_y = 1;
s->widgetlist = LL_new();
if (!s->widgetlist) {
if (s->widgetlist == NULL) {
report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
free(s);
free(s->id);
return NULL;
}
@@ -92,6 +104,12 @@ screen_create(char *id, Client *client)
return s;
}
/** Destroy a screen.
* \param s Screen to destroy.
* \retval <0 Error; no screen given.
* \retval 0 Success.
*/
int
screen_destroy(Screen *s)
{
@@ -108,17 +126,30 @@ screen_destroy(Screen *s)
widget_destroy(w);
}
LL_Destroy(s->widgetlist);
s->widgetlist = NULL;
if (s->id)
if (s->id != NULL) {
free(s->id);
if (s->name)
s->id = NULL;
}
if (s->name != NULL) {
free(s->name);
s->name = NULL;
}
free(s);
s = NULL;
return 0;
}
/** Add a widget to a screen.
* \param s Screen to add the widget \c w to.
* \param w Widget to be added to \c s.
* \retval <0 Error.
* \retval 0 Success.
*/
int
screen_add_widget(Screen *s, Widget *w)
{
@@ -129,6 +160,13 @@ screen_add_widget(Screen *s, Widget *w)
return 0;
}
/** Remove a widget from a screen.
* \param s Screen to remove the widget \c w from.
* \param w Widget to be removed from \c s.
* \retval <0 Error.
* \retval 0 Success.
*/
int
screen_remove_widget(Screen *s, Widget *w)
{
@@ -139,6 +177,12 @@ screen_remove_widget(Screen *s, Widget *w)
return 0;
}
/** Find a widget on a screen by its id.
* \param s Screen where to look for the widget.
* \param id Identifier of the widget.
* \return Pointerr to the widget; \c NULL if widget was not found or error.
*/
Widget *
screen_find_widget(Screen *s, char *id)
{
@@ -151,7 +195,7 @@ screen_find_widget(Screen *s, char *id)
debug(RPT_DEBUG, "%s(s=[%.40s], id=\"%.40s\")", __FUNCTION__, s->id, id);
for (w = LL_GetFirst(s->widgetlist); w; w = LL_GetNext(s->widgetlist)) {
for (w = LL_GetFirst(s->widgetlist); w != NULL; w = LL_GetNext(s->widgetlist)) {
if (0 == strcmp(w->id, id)) {
debug(RPT_DEBUG, "%s: Found %s", __FUNCTION__, id);
return w;
@@ -159,7 +203,7 @@ screen_find_widget(Screen *s, char *id)
/* Search subscreens recursively */
if (w->type == WID_FRAME) {
w = widget_search_subs(w, id);
if (w)
if (w != NULL)
return w;
}
}
@@ -167,6 +211,11 @@ screen_find_widget(Screen *s, char *id)
return NULL;
}
/** Convert a priority name to the priority id.
* \param priname Name of the screen priority.
* \return Priority id associatited with \c priname.
*/
Priority
screen_pri_name_to_pri(char *priname)
{
@@ -182,6 +231,11 @@ screen_pri_name_to_pri(char *priname)
return pri;
}
/** Convert a priority id to the associated name.
* \param pri Priority id.
* \return Priority name associated with \c pri.
*/
char *
screen_pri_to_pri_name(Priority pri)
{
+1
View File
@@ -1,4 +1,5 @@
/** \file server/screen.h
* Public interface to the screen management methods.
*/
/* This file is part of LCDd, the lcdproc server.