From f586ae4a0a8d081c46f10f4cb4c27840d611edec Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sat, 16 Apr 2022 11:51:37 -0700 Subject: Stop leaking XkbAtomGetString allocations XkbAtomGetString is a wrapper around XGetAtomName, and like XGetAtomName returns a newly allocated string (or NULL) on every call, so we should free those strings when done instead of leaking them. Reported by Oracle Parfait static analyser: Error: Memory leak Memory leak [memory-leak] (CWE 401): Memory leak of pointer pointer allocated with XkbAtomGetString(state->dpy, state->geom->name) at line 720 of app/xkbprint/psgeom.c in function 'PSProlog'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' pointer allocated at line 612 of app/xkbprint/psgeom.c in function 'PSProlog' with XkbAtomGetString(state->dpy, state->geom->name) pointer leaks when (i + 1) >= state->geom->num_colors at line 694 and (i + 1) >= state->geom->num_shapes at line 712. Memory leak [memory-leak] (CWE 401): Memory leak of pointer pointer allocated with XkbAtomGetString(state->dpy, state->geom->name) at line 720 of app/xkbprint/psgeom.c in function 'PSProlog'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' pointer allocated at line 657 of app/xkbprint/psgeom.c in function 'PSProlog' with XkbAtomGetString(state->dpy, state->geom->name) pointer leaks when (i + 1) >= state->geom->num_colors at line 694 and (i + 1) >= state->geom->num_shapes at line 712. Error: Memory leak Memory leak [memory-leak] (CWE 401): Memory leak of pointer name allocated with XkbAtomGetString(state->dpy, geom->name) at line 962 of app/xkbprint/psgeom.c in function 'PSPageTrailer'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' name allocated at line 845 of app/xkbprint/psgeom.c in function 'PSPageTrailer' with XkbAtomGetString(state->dpy, geom->name) name leaks when (p + 1) >= state->args->copies at line 926. Memory leak [memory-leak] (CWE 401): Memory leak of pointer sName allocated with XkbAtomGetString(state->dpy, sAtom) at line 962 of app/xkbprint/psgeom.c in function 'PSPageTrailer'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' sName allocated at line 877 of app/xkbprint/psgeom.c in function 'PSPageTrailer' with XkbAtomGetString(state->dpy, sAtom) sName leaks when (p + 1) >= state->args->copies at line 926. Memory leak [memory-leak] (CWE 401): Memory leak of pointer sName allocated with XkbAtomGetString(state->dpy, sAtom) at line 962 of app/xkbprint/psgeom.c in function 'PSPageTrailer'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' sName allocated at line 910 of app/xkbprint/psgeom.c in function 'PSPageTrailer' with XkbAtomGetString(state->dpy, sAtom) sName leaks when (p + 1) >= state->args->copies at line 926. Error: Memory leak Memory leak [memory-leak] (CWE 401): Memory leak of pointer dname allocated with XkbAtomGetString(xkb->dpy, doodad.any.name) at line 1068 of app/xkbprint/psgeom.c in function 'PSDoodad'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' dname allocated at line 974 of app/xkbprint/psgeom.c in function 'PSDoodad' with XkbAtomGetString(xkb->dpy, doodad.any.name) Memory leak [memory-leak] (CWE 401): Memory leak of pointer name allocated with XkbAtomGetString(...) at line 1068 of app/xkbprint/psgeom.c in function 'PSDoodad'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' name allocated at line 980 of app/xkbprint/psgeom.c in function 'PSDoodad' with XkbAtomGetString(...) Memory leak [memory-leak] (CWE 401): Memory leak of pointer name allocated with XkbAtomGetString(...) at line 1068 of app/xkbprint/psgeom.c in function 'PSDoodad'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' name allocated at line 1044 of app/xkbprint/psgeom.c in function 'PSDoodad' with XkbAtomGetString(...) Memory leak [memory-leak] (CWE 401): Memory leak of pointer name allocated with XkbAtomGetString(...) at line 1068 of app/xkbprint/psgeom.c in function 'PSDoodad'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' name allocated at line 1058 of app/xkbprint/psgeom.c in function 'PSDoodad' with XkbAtomGetString(...) Error: Memory leak Memory leak [memory-leak] (CWE 401): Memory leak of pointer pointer allocated with XkbAtomGetString(dpy, section->name) at line 2063 of app/xkbprint/psgeom.c in function 'PSSection'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' pointer allocated at line 1907 of app/xkbprint/psgeom.c in function 'PSSection' with XkbAtomGetString(dpy, section->name) pointer leaks when (k + 1) >= (section->rows + )->num_keys at line 1936 Memory leak [memory-leak] (CWE 401): Memory leak of pointer pointer allocated with XkbAtomGetString(dpy, shape->name) at line 2063 of app/xkbprint/psgeom.c in function 'PSSection'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' pointer allocated at line 1945 of app/xkbprint/psgeom.c in function 'PSSection' with XkbAtomGetString(dpy, shape->name) pointer leaks when (k + 1) >= (section->rows + )->num_keys at line 1936 and (r + 1) >= section->num_rows at line 1925. Memory leak [memory-leak] (CWE 401): Memory leak of pointer pointer allocated with XkbAtomGetString(dpy, shape->name) at line 2063 of app/xkbprint/psgeom.c in function 'PSSection'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' pointer allocated at line 1951 of app/xkbprint/psgeom.c in function 'PSSection' with XkbAtomGetString(dpy, shape->name) pointer leaks when (k + 1) >= (section->rows + )->num_keys at line 1936 and (r + 1) >= section->num_rows at line 1925. Memory leak [memory-leak] (CWE 401): Memory leak of pointer pointer allocated with XkbAtomGetString(dpy, shape->name) at line 2063 of app/xkbprint/psgeom.c in function 'PSSection'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' pointer allocated at line 1960 of app/xkbprint/psgeom.c in function 'PSSection' with XkbAtomGetString(dpy, shape->name) pointer leaks when (k + 1) >= (section->rows + )->num_keys at line 1936 and (r + 1) >= section->num_rows at line 1925. Memory leak [memory-leak] (CWE 401): Memory leak of pointer pointer allocated with XkbAtomGetString(dpy, shape->name) at line 2063 of app/xkbprint/psgeom.c in function 'PSSection'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' pointer allocated at line 1966 of app/xkbprint/psgeom.c in function 'PSSection' with XkbAtomGetString(dpy, shape->name) pointer leaks when (k + 1) >= (section->rows + )->num_keys at line 1936 and (r + 1) >= section->num_rows at line 1925. Error: Memory leak Memory leak [memory-leak] (CWE 401): Memory leak of pointer name allocated with XkbAtomGetString(...) at line 2131 of app/xkbprint/psgeom.c in function 'GeometryToPostScript'. Allocated value returned to caller at line 207 of lib/libxkbfile/src/xkbatom.c in function 'XkbAtomGetString' name allocated at line 2110 of app/xkbprint/psgeom.c in function 'GeometryToPostScript' with XkbAtomGetString(...) name leaks when name != NULL at line 2111. Signed-off-by: Alan Coopersmith --- psgeom.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 23 deletions(-) diff --git a/psgeom.c b/psgeom.c index c43dd10..f4072da 100644 --- a/psgeom.c +++ b/psgeom.c @@ -265,7 +265,11 @@ PSShapeDef(FILE *out, PSState *state, XkbShapePtr shape) int o, p; XkbOutlinePtr ol; - fprintf(out, "/%s {\n", XkbAtomGetString(state->dpy, shape->name)); + { + char *a = XkbAtomGetString(state->dpy, shape->name); + fprintf(out, "/%s {\n", a); + XFree(a); + } fprintf(out, " gsave translate rotate /SOLID exch def\n"); for (o = 0, ol = shape->outlines; o < shape->num_outlines; o++, ol++) { XkbPointPtr pt; @@ -607,9 +611,11 @@ PSProlog(FILE *out, PSState *state) fprintf(out, "%%!PS-Adobe-2.0\n" "%%%%Creator: xkbprint\n"); - if (state->geom->name != None) - fprintf(out, "%%%%Title: %s\n", - XkbAtomGetString(state->dpy, state->geom->name)); + if (state->geom->name != None) { + char *a = XkbAtomGetString(state->dpy, state->geom->name); + fprintf(out, "%%%%Title: %s\n", a); + XFree(a); + } fprintf(out, "%%%%BoundingBox: (atend)\n" "%%%%Pages: 1\n" @@ -652,9 +658,11 @@ PSProlog(FILE *out, PSState *state) fprintf(out, "%%!PS-Adobe-2.0 EPSF-2.0\n"); fprintf(out, "%%%%BoundingBox: 0 0 %d %d\n", w, h); fprintf(out, "%%%%Creator: xkbprint\n"); - if (state->geom->name != None) - fprintf(out, "%%%%Title: %s\n", - XkbAtomGetString(state->dpy, state->geom->name)); + if (state->geom->name != None) { + char *a = XkbAtomGetString(state->dpy, state->geom->name); + fprintf(out, "%%%%Title: %s\n", a); + XFree(a); + } fprintf(out, "%%%%Pages: 1\n"); fprintf(out, "%%%%EndComments\n"); fprintf(out, "%%%%BeginProlog\n"); @@ -841,8 +849,10 @@ PSPageTrailer(FILE *out, PSState *state) if ((!state->args->wantEPS) && ((state->kbPerPage == 1) || ((state->nPages & 1) == 1) || (state->nPages == state->totalKB))) { + char *a1 = NULL; + if ((name == NULL) && (geom->name != None)) - name = XkbAtomGetString(state->dpy, geom->name); + name = a1 = XkbAtomGetString(state->dpy, geom->name); baseline = 16; if ((name != NULL) || (state->args->label == LABEL_SYMBOLS)) { @@ -852,6 +862,7 @@ PSPageTrailer(FILE *out, PSState *state) if (state->args->label == LABEL_SYMBOLS) { char buf[40], *lbuf; const char *sName = NULL; + char *a2 = NULL; Atom sAtom; if (state->args->nLabelGroups == 1) @@ -874,7 +885,7 @@ PSPageTrailer(FILE *out, PSState *state) else sAtom = None; if (sAtom != None) - sName = XkbAtomGetString(state->dpy, sAtom); + sName = a2 = XkbAtomGetString(state->dpy, sAtom); if (sName == NULL) sName = "(unknown)"; @@ -888,6 +899,7 @@ PSPageTrailer(FILE *out, PSState *state) fprintf(out, "1 -1 scale (%s) show 1 -1 scale\n", lbuf); baseline += 16; free(lbuf); + XFree(a2); } if (name != NULL) { fprintf(out, "kbx kbdscalewidth 0 (%s) centeroffset pop add\n", @@ -896,10 +908,13 @@ PSPageTrailer(FILE *out, PSState *state) fprintf(out, " moveto\n"); fprintf(out, "1 -1 scale (%s) show 1 -1 scale\n", name); baseline += 16; + name = NULL; + XFree(a1); } if (state->args->label == LABEL_KEYCODE) { const char *sName = NULL; char *lbuf; + char *a3 = NULL; Atom sAtom; if (xkb->names != NULL) @@ -907,7 +922,7 @@ PSPageTrailer(FILE *out, PSState *state) else sAtom = None; if (sAtom != None) - sName = XkbAtomGetString(state->dpy, sAtom); + sName = a3 = XkbAtomGetString(state->dpy, sAtom); if (sName == NULL) sName = "(unknown)"; @@ -921,6 +936,7 @@ PSPageTrailer(FILE *out, PSState *state) fprintf(out, "1 -1 scale (%s) show 1 -1 scale\n", lbuf); baseline += 16; free(lbuf); + XFree(a3); } if (state->args->copies > 1) { for (p = 1; p < state->args->copies; p++) @@ -967,17 +983,18 @@ PSDoodad(FILE *out, PSState *state, XkbDoodadPtr doodad) { XkbDescPtr xkb; const char *name, *dname; + char *a1 = NULL, *a2 = NULL; int sz, leading; xkb = state->xkb; if (doodad->any.name != None) - dname = XkbAtomGetString(xkb->dpy, doodad->any.name); + dname = a1 = XkbAtomGetString(xkb->dpy, doodad->any.name); else dname = "NoName"; switch (doodad->any.type) { case XkbOutlineDoodad: case XkbSolidDoodad: - name = XkbAtomGetString(xkb->dpy, + name = a2 = XkbAtomGetString(xkb->dpy, XkbShapeDoodadShape(xkb->geom, &doodad->shape)->name); if (state->args->wantColor) { @@ -997,6 +1014,8 @@ PSDoodad(FILE *out, PSState *state, XkbDoodadPtr doodad) doodad->shape.angle, doodad->shape.left, doodad->shape.top, name, dname); } + name = NULL; + XFree(a2); break; case XkbTextDoodad: fprintf(out, "%% Doodad %s\n", dname); @@ -1041,7 +1060,7 @@ PSDoodad(FILE *out, PSState *state, XkbDoodadPtr doodad) PSGRestore(out, state); break; case XkbIndicatorDoodad: - name = XkbAtomGetString(xkb->dpy, + name = a2 = XkbAtomGetString(xkb->dpy, XkbIndicatorDoodadShape(xkb->geom, &doodad->indicator)-> name); @@ -1053,9 +1072,11 @@ PSDoodad(FILE *out, PSState *state, XkbDoodadPtr doodad) } fprintf(out, "false 0 %d %d %s %% Doodad %s\n", doodad->indicator.left, doodad->indicator.top, name, dname); + name = NULL; + XFree(a2); break; case XkbLogoDoodad: - name = XkbAtomGetString(xkb->dpy, + name = a2 = XkbAtomGetString(xkb->dpy, XkbLogoDoodadShape(xkb->geom, &doodad->logo)->name); if (state->args->wantColor) @@ -1063,8 +1084,11 @@ PSDoodad(FILE *out, PSState *state, XkbDoodadPtr doodad) fprintf(out, "false %d %d %d %s %% Doodad %s\n", doodad->shape.angle, doodad->shape.left, doodad->shape.top, name, dname); + name = NULL; + XFree(a2); break; } + XFree(a1); return; } @@ -1903,8 +1927,17 @@ PSSection(FILE *out, PSState *state, XkbSectionPtr section) xkb = state->xkb; dpy = xkb->dpy; - fprintf(out, "%% Begin Section '%s'\n", (section->name != None ? - XkbAtomGetString(dpy, section-> name) : "NoName")); + { + const char *section_name; + char *atom_name = NULL; + + if (section->name != None) + section_name = atom_name = XkbAtomGetString(dpy, section->name); + else + section_name = "NoName"; + fprintf(out, "%% Begin Section '%s'\n", section_name); + XFree(atom_name); + } PSGSave(out, state); fprintf(out, "%d %d translate\n", section->left, section->top); if (section->angle != 0) @@ -1925,7 +1958,6 @@ PSSection(FILE *out, PSState *state, XkbSectionPtr section) for (r = 0, row = section->rows; r < section->num_rows; r++, row++) { int k; XkbKeyPtr key; - XkbShapePtr shape; if (row->vertical) offset = row->top; @@ -1934,21 +1966,22 @@ PSSection(FILE *out, PSState *state, XkbSectionPtr section) fprintf(out, "%% Begin %s %d\n", row->vertical ? "column" : "row", r + 1); for (k = 0, key = row->keys; k < row->num_keys; k++, key++) { - shape = XkbKeyShape(xkb->geom, key); + XkbShapePtr shape = XkbKeyShape(xkb->geom, key); + char *shape_name = XkbAtomGetString(dpy, shape->name); + offset += key->gap; if (row->vertical) { if (state->args->wantColor) { if (key->color_ndx != state->white) { PSSetColor(out, state, key->color_ndx); fprintf(out, "true 0 %d %d %s %% %s\n", - row->left, offset, - XkbAtomGetString(dpy, shape->name), + row->left, offset, shape_name, XkbKeyNameText(key->name.name, XkbMessage)); } PSSetColor(out, state, state->black); } fprintf(out, "false 0 %d %d %s %% %s\n", row->left, offset, - XkbAtomGetString(dpy, shape->name), + shape_name, XkbKeyNameText(key->name.name, XkbMessage)); offset += shape->bounds.y2; } @@ -1957,16 +1990,17 @@ PSSection(FILE *out, PSState *state, XkbSectionPtr section) if (key->color_ndx != state->white) { PSSetColor(out, state, key->color_ndx); fprintf(out, "true 0 %d %d %s %% %s\n", offset, - row->top, XkbAtomGetString(dpy, shape->name), + row->top, shape_name, XkbKeyNameText(key->name.name, XkbMessage)); } PSSetColor(out, state, state->black); } fprintf(out, "false 0 %d %d %s %% %s\n", offset, row->top, - XkbAtomGetString(dpy, shape->name), + shape_name, XkbKeyNameText(key->name.name, XkbMessage)); offset += shape->bounds.x2; } + XFree(shape_name); } } for (r = 0, row = section->rows; r < section->num_rows; r++, row++) { @@ -2110,8 +2144,10 @@ GeometryToPostScript(FILE *out, XkbFileInfo *pResult, XKBPrintArgs *args) name = XkbAtomGetString(state.dpy, draw->u.doodad->any.name); if ((name != NULL) && (uStrCaseEqual(name, "edges"))) { dfltBorder = False; + XFree(name); break; } + XFree(name); } } for (i = 0; i < state.totalKB; i++) { -- cgit v1.2.3