Browse Source

A bit safer

Piotr Czajkowski 5 years ago
parent
commit
c5c8ea57ee
3 changed files with 26 additions and 0 deletions
  1. 15 0
      comments.c
  2. 6 0
      xmlbuff.c
  3. 5 0
      zip.c

+ 15 - 0
comments.c

@@ -18,6 +18,11 @@ char* anonymizeAuthor(binn *anonAuthors, const xmlChar *authorName) {
 		return newName;
 	
 	newName = malloc(NAME_LENGTH);
+	if (newName == NULL) {
+		printf("Couldn't allocate memory for %s!\n", name);
+		return NULL;
+	}
+
 	snprintf(newName, NAME_LENGTH,"Author%d", ++authorsCount);
 	binn_object_set_str(anonAuthors, name, newName);
 	binn_object_set_str(anonAuthors, newName, name);
@@ -40,6 +45,11 @@ int anonymizeAuthors(const xmlXPathObjectPtr authors) {
 		xmlChar *authorName = (xmlChar*)"";		
 		authorName = xmlNodeGetContent(authors->nodesetval->nodeTab[i]);
 		char *anonAuthor = anonymizeAuthor(anonAuthors, authorName);
+		if (anonAuthor == NULL) {
+			printf("Couldn't anonymize %s!\n", authorName);
+			return 0;
+		}
+		
 		xmlNodeSetContent(authors->nodesetval->nodeTab[i], (xmlChar*)anonAuthor);
 		xmlFree(authorName);
 	}
@@ -63,6 +73,11 @@ binn *readAuthors() {
         fseek(fp, 0, SEEK_SET);
 
         data = malloc(fsize + 1);
+	if (data == NULL) {
+		puts("Couldn't allocate memory for data!");
+		return NULL;
+	}
+	
         fread(data, fsize, 1, fp);
         fclose(fp);
 

+ 6 - 0
xmlbuff.c

@@ -1,8 +1,14 @@
 #include <stdlib.h>
+#include <stdio.h>
 #include "xmlbuff.h"
 
 XMLBuff *XMLBuffNew(void) {
 	XMLBuff *out = malloc(sizeof(XMLBuff));
+	if (out == NULL) {
+		puts("Couldn't allocate memory for XMLBuff!");
+		return NULL;
+	}
+	
 	*out = (XMLBuff){ .data=NULL };                          
 	return out;
 }

+ 5 - 0
zip.c

@@ -28,6 +28,11 @@ int rewriteZIP(struct archive *archiveIn, struct archive *archiveOut) {
 
 		if (strcmp(commentsFile, path) == 0){
 			XMLBuff *comments = XMLBuffNew();
+			if (comments == NULL) {
+				puts("Couldn't obtain comments!");
+				return 0;
+			}
+			
 			*comments = (XMLBuff){.data=buf, .size=size, .name=path};
 
 			if (!processComments(archiveOut, comments)) return 0;