12bd665
diff --git a/utils/cups-browsed.c b/utils/cups-browsed.c
12bd665
index 3cb1f71..db822af 100644
12bd665
--- a/utils/cups-browsed.c
12bd665
+++ b/utils/cups-browsed.c
12bd665
@@ -7200,6 +7200,10 @@ create_remote_printer_entry (const char *queue_name,
12bd665
      in a row during creation of this printer's queue */
12bd665
   p->timeouted = 0;
12bd665
 
12bd665
+  /* Initialize nickname array for *Nickname directive from PPD
12bd665
+   * - either from CUPS server or from our PPD generator */
12bd665
+  p->nickname = NULL;
12bd665
+
12bd665
   /* Remote CUPS printer or local queue remaining from previous cups-browsed
12bd665
      session */
12bd665
   /* is_cups_queue: -1: Unknown, 0: IPP printer, 1: Remote CUPS queue,
12bd665
@@ -7215,7 +7219,6 @@ create_remote_printer_entry (const char *queue_name,
12bd665
        remote CUPS server gets used. So we will not generate a PPD file
12bd665
        or interface script at this point. */
12bd665
     p->netprinter = 0;
12bd665
-    p->nickname = NULL;
12bd665
     if (p->uri[0] != '\0') {
12bd665
       p->prattrs = get_printer_attributes(p->uri, NULL, 0, NULL, 0, 1);
12bd665
       debug_log_out(get_printer_attributes_log);
12bd665
@@ -7620,7 +7623,7 @@ gboolean update_cups_queues(gpointer unused) {
12bd665
   time_t        current_time;
12bd665
   int           i, ap_remote_queue_id_line_inserted,
12bd665
                 want_raw, num_cluster_printers = 0;
12bd665
-  char          *disabled_str, *ptr;
12bd665
+  char          *disabled_str;
12bd665
   char          *ppdfile, *ifscript;
12bd665
   int           fd = 0;  /* Script file descriptor */
12bd665
   char          tempfile[1024];  /* Temporary file */
12bd665
@@ -8189,7 +8192,6 @@ gboolean update_cups_queues(gpointer unused) {
12bd665
 	    debug_printf("Generated Default Attributes for local queue %s\n",
12bd665
 			 p->queue_name);
12bd665
 	  }
12bd665
-	  p->nickname = NULL;
12bd665
 	  if (ppdfile == NULL) {
12bd665
 	    /* If we do not want CUPS-generated PPDs or we cannot obtain a
12bd665
 	       CUPS-generated PPD, for example if CUPS does not create a 
12bd665
@@ -8476,7 +8478,6 @@ gboolean update_cups_queues(gpointer unused) {
12bd665
 			 p->queue_name, p->uri);
12bd665
 	    goto cannot_create;
12bd665
 	  }
12bd665
-	  p->nickname = NULL;
12bd665
 	  num_cluster_printers = 0;
12bd665
 	  for (s = (remote_printer_t *)cupsArrayFirst(remote_printers);
12bd665
 	       s; s = (remote_printer_t *)cupsArrayNext(remote_printers)) {
12bd665
@@ -8683,14 +8684,48 @@ gboolean update_cups_queues(gpointer unused) {
12bd665
 	     manipulations of the print queue have replaced the PPD.
12bd665
 	     Check whether nickname is defined too */
12bd665
 	  if (!strncmp(line, "*NickName:", 10) && p->nickname == NULL) {
12bd665
+	    char *ptr = NULL;
12bd665
+	    char *end_ptr = NULL;
12bd665
+	    int nickname_len = 0;
12bd665
+
12bd665
 	    ptr = strchr(line, '"');
12bd665
-	    if (ptr) {
12bd665
-	      ptr ++;
12bd665
-	      p->nickname = strdup(ptr);
12bd665
-	      ptr = strchr(p->nickname, '"');
12bd665
-	      if (ptr)
12bd665
-		*ptr = '\0';
12bd665
+
12bd665
+	    if (ptr == NULL)
12bd665
+	    {
12bd665
+	      debug_printf("Malformed *Nickname directive in PPD - no double quote in line.\n");
12bd665
+	      continue;
12bd665
+	    }
12bd665
+
12bd665
+	    ptr ++;
12bd665
+	    end_ptr = strchr(ptr, '"');
12bd665
+
12bd665
+	    if (end_ptr == NULL)
12bd665
+	    {
12bd665
+	      debug_printf("Malformed *Nickname directive in PPD - no ending double quote\n");
12bd665
+	      continue;
12bd665
 	    }
12bd665
+
12bd665
+	    /* both pointers are null terminated, because cupsFileGets() puts
12bd665
+	     * a null terminator into returned buffer with one line
12bd665
+	     * here as 'line' array) and those two pointers points on two places
12bd665
+	     * in the 'line' array.
12bd665
+	     */
12bd665
+	    nickname_len = strlen(ptr) - strlen(end_ptr);
12bd665
+
12bd665
+	    if (nickname_len == 0)
12bd665
+	    {
12bd665
+	      debug_printf("Malformed *Nickname directive in PPD - empty nickname.\n");
12bd665
+	      continue;
12bd665
+	    }
12bd665
+
12bd665
+	    /* alloc one more space for null terminator, calloc() will initialize
12bd665
+	     * it to null automatically, so then we only copy a string with 'nickname_len'
12bd665
+	     * length to get a proper null terminated p->nickname.
12bd665
+	     */
12bd665
+	    p->nickname = (char*)calloc(nickname_len + 1, sizeof(char));
12bd665
+
12bd665
+	    if (p->nickname != NULL)
12bd665
+	      strncpy(p->nickname, ptr, nickname_len);
12bd665
 	  }
12bd665
 	}
12bd665
 	cupsFilePrintf(out,"*cupsFilter2: \"application/vnd.cups-pdf application/pdf 0 -\"\n");