[MPlayer-dev-eng] Re: [PATCH] fix stack usage in libvo/font_load_ft.c

Tobias Diedrich ranma at tdiedrich.de
Fri Jul 21 22:44:31 CEST 2006


Alternative version, uses goto to avoid duplicated free calls.
I also forgot to check for malloc failure in the previous version.

-- 
Tobias						PGP: http://9ac7e0bc.uguu.de
-------------- next part --------------
Index: mplayer/libvo/font_load_ft.c
===================================================================
--- mplayer/libvo/font_load_ft.c	(revision 19150)
+++ mplayer/libvo/font_load_ft.c	(working copy)
@@ -958,8 +958,8 @@
 
     FT_Face face;
 
-    FT_ULong my_charset[MAX_CHARSET_SIZE]; /* characters we want to render; Unicode */
-    FT_ULong my_charcodes[MAX_CHARSET_SIZE]; /* character codes in 'encoding' */
+    FT_ULong *my_charset = malloc(MAX_CHARSET_SIZE * sizeof(FT_ULong)); /* characters we want to render; Unicode */
+    FT_ULong *my_charcodes = malloc(MAX_CHARSET_SIZE * sizeof(FT_ULong)); /* character codes in 'encoding' */
 
     char *charmap = "ucs-4";
     int err;
@@ -972,6 +972,12 @@
     float subtitle_font_ppem;
     float osd_font_ppem;
 
+    if (my_charset == NULL ||
+        my_charcodes == NULL) {
+	mp_msg(MSGT_OSD, MSGL_ERR, "subtitle font: malloc failed.\n");
+	return NULL;
+    }
+
     switch (subtitle_autoscale) {
     case 1:
 	movie_size = movie_height;
@@ -1004,7 +1010,7 @@
     }
 
     desc = init_font_desc();
-    if(!desc) return NULL;
+    if(!desc) goto err1;
 
 //    t=GetTimer();
 
@@ -1029,11 +1035,10 @@
 
     if (charset_size < 0) {
 	mp_msg(MSGT_OSD, MSGL_ERR, "subtitle font: prepare_charset failed.\n");
-	free_font_desc(desc);
-	return NULL;
+	goto err2;
     }
 #else
-    return NULL;
+    goto err2;
 #endif
 
 //    fprintf(stderr, "fg: prepare t = %lf\n", GetTimer()-t);
@@ -1044,18 +1049,14 @@
 
     if (err) {
 	mp_msg(MSGT_OSD, MSGL_ERR, "Cannot prepare subtitle font.\n");
-	free_font_desc(desc);
-	return NULL;
+	goto err2;
     }
 
 gen_osd:
 
     /* generate the OSD font */
     err = load_osd_face(&face);
-    if (err) {
-	free_font_desc(desc);
-	return NULL;
-    }
+    if (err) goto err2;
     desc->face_cnt++;
 
     err = prepare_font(desc, face, osd_font_ppem, desc->face_cnt-1,
@@ -1064,16 +1065,14 @@
     
     if (err) {
 	mp_msg(MSGT_OSD, MSGL_ERR, "Cannot prepare OSD font.\n");
-	free_font_desc(desc);
-	return NULL;
+	goto err2;
     }
 
     err = generate_tables(desc, subtitle_font_thickness, subtitle_font_radius);
     
     if (err) {
 	mp_msg(MSGT_OSD, MSGL_ERR, "Cannot generate tables.\n");
-	free_font_desc(desc);
-	return NULL;
+	goto err2;
     }
 
     // final cleanup
@@ -1091,7 +1090,16 @@
 	    desc->font[i] = desc->font[j];
 	}
     }
+    free(my_charset);
+    free(my_charcodes);
     return desc;
+
+err2:
+    free_font_desc(desc);
+err1:
+    free(my_charset);
+    free(my_charcodes);
+    return NULL;
 }
 
 int init_freetype(void)


More information about the MPlayer-dev-eng mailing list