libprint: Fix using auto_ptr to array.

Change-Id: If22530aba951e2856fc03cf59de167353db42604
Reviewed-on: https://review.haiku-os.org/727
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
Reviewed-by: Jérôme Duval <jerome.duval@gmail.com>
This commit is contained in:
Murai Takashi 2018-11-23 06:11:56 +09:00 committed by Jérôme Duval
parent 2c5c300420
commit 6d2736585e

View File

@ -7,13 +7,14 @@
#include "PrinterDriver.h" #include "PrinterDriver.h"
#include <fs_attr.h> // for attr_info #include <fs_attr.h> // for attr_info
#include <DataIO.h>
#include <File.h> #include <File.h>
#include <FindDirectory.h> #include <FindDirectory.h>
#include <Message.h> #include <Message.h>
#include <Node.h> #include <Node.h>
#include <Path.h> #include <Path.h>
#include <StackOrHeapArray.h>
#include <String.h> #include <String.h>
#include <memory> // for auto_ptr
#include "AboutBox.h" #include "AboutBox.h"
#include "AddPrinterDlg.h" #include "AddPrinterDlg.h"
@ -175,14 +176,14 @@ bool
PrinterDriver::_ReadSettings(const char* attrName, BMessage* settings) PrinterDriver::_ReadSettings(const char* attrName, BMessage* settings)
{ {
attr_info info; attr_info info;
char* data;
ssize_t size; ssize_t size;
settings->MakeEmpty(); settings->MakeEmpty();
if (fSpoolFolder->GetAttrInfo(attrName, &info) == B_OK && info.size > 0) { if (fSpoolFolder->GetAttrInfo(attrName, &info) == B_OK && info.size > 0) {
data = new char[info.size]; BStackOrHeapArray<char, 0> data(info.size);
auto_ptr<char> _data(data); if (!data.IsValid())
return false;
size = fSpoolFolder->ReadAttr(attrName, B_MESSAGE_TYPE, 0, data, info.size); size = fSpoolFolder->ReadAttr(attrName, B_MESSAGE_TYPE, 0, data, info.size);
if (size == info.size && settings->Unflatten(data) == B_OK) { if (size == info.size && settings->Unflatten(data) == B_OK) {
return true; return true;
@ -197,15 +198,13 @@ PrinterDriver::_WriteSettings(const char* attrName, BMessage* settings)
{ {
if (settings == NULL || settings->what != 'okok') return; if (settings == NULL || settings->what != 'okok') return;
size_t size; status_t status;
char* data; BMallocIO data;
status = settings->Flatten(&data);
size = settings->FlattenedSize(); if (status == B_OK) {
data = new char[size]; fSpoolFolder->WriteAttr(attrName, B_MESSAGE_TYPE, 0, data.Buffer(),
auto_ptr<char> _data(data); data.BufferLength());
if (data != NULL && settings->Flatten(data, size) == B_OK) {
fSpoolFolder->WriteAttr(attrName, B_MESSAGE_TYPE, 0, data, size);
} }
} }